2016-03-25 43 views
2

我目前在使用ASP .NET Core的門戶網站工作。 其中一個要求是創建Azure AD用戶,途中發現了一些問題。如何在ASP .NET Core中創建Azure AD用戶?

首先,試圖用GraphClient SDK我得到這些編譯錯誤時:

Severity Code Description Project File Line Suppression State 
Error CS0012 The type 'IList<>' is defined in an assembly that is not referenced. 
You must add a reference to assembly 'System.Runtime, Version=4.0.0.0, 
Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. PTIWebPortal.Packages.Cloud.DNX 4.6 
D:\Eduardo\PTI Projects\PTIPortal\Portal\PTIPortal\PTIWebPortal.Packages.Cloud\CloudUserManager.cs 40 Active 

這一個嘗試設置對象的OtherMails財產 newUser.OtherMails =新System.Collections.Generic發生時.LIST();

另一個編譯錯誤是

Severity Code Description Project File Line Suppression State 
Error CS0012 The type 'Uri' is defined in an assembly that is not referenced. 
You must add a reference to assembly 'System.Runtime, Version=4.0.0.0, 
Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. 
PTIWebPortal.Packages.Cloud.DNX 4.6 
D:\Eduardo\PTI Projects\PTIPortal\Portal\PTIPortal\PTIWebPortal.Packages.Cloud\CloudUserManager.cs 43 Active 

試圖實例的ActiveDirectoryClient ActiveDirectoryClient的AdClient =新ActiveDirectoryClient(serviceRoot,空)時,這發生對象;

我覺得這兩個都是由於軟件開發工具包errores尚未與.NET核心完全兼容,因爲已經有是我已經使用URI類型 這是一個不同的版本

//生成由.NET反射從C:\ Windows \ Microsoft.Net \程序集\ GAC_MSIL \系統\ v4.0_4.0.0.0__b77a5c561934e089 \ System.dll

我花了太多的時間,所以我決定嘗試使用Microsoft Graph,但是在將讀寫目錄數據添加到Azure AD中的應用程序後,即使在 之後,我仍然收到「Forbidden」響應這是針對

public static readonly string CreateUserUrl = @"https://graph.microsoft.com/{0}/users"; 
public static async Task<UserInfo> CreateUser(string accessToken, UserInfo pUser) 
     { 
      using (var client = new HttpClient()) 
      { 
       using (var request = new HttpRequestMessage(HttpMethod.Post, Settings.CreateUserUrl.v10Version())) 
       { 
        request.Headers.Accept.Add(Json); 
        request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken); 
        var userData = new 
        { 
         accountEnabled = true, 
         displayName = pUser.DisplayName, 
         mailNickname = pUser.Username, 
         passwordProfile = new 
         { 
          password = pUser.Password, 
          forceChangePasswordNextSignIn = false 
         }, 
         userPrincipalName = string.Format("{0}@{1}", pUser.Username, pUser.Domain) 
        }; 
        string serializedData = JsonConvert.SerializeObject(userData); 
        request.Content = new StringContent(serializedData, System.Text.Encoding.UTF8, "application/json"); 
        //https://msdn.microsoft.com/en-us/library/azure/ad/graph/api/users-operations 
        //http://stackoverflow.com/questions/35845541/microsoft-graph-rest-api-add-attachment-to-email-using-c-sharp-asp-net-mvc 

        using (var response = await client.SendAsync(request)) 
        { 
         if (response.StatusCode == HttpStatusCode.OK) 
         { 
          var json = JObject.Parse(await response.Content.ReadAsStringAsync()); 
          //myInfo.DisplayName = json?["displayName"]?.ToString(); 
          //myInfo.MailAddress = json?["mail"]?.ToString().Trim().Replace(" ", string.Empty); 
          //myInfo.Department = json?["department"]?.ToString(); 
          //myInfo.PhotoBytes = await GetUserPhotoAsync(accessToken, json?["userPrincipalName"]?.ToString()); 
         } 
        } 
       } 
      } 
      return pUser; 
     } 

注意當前代碼:我已經能夠登錄爲Azure的AD用戶,我也能使用Microsoft圖表來獲取信息。

任何想法,我可以做些什麼來解決這兩個問題?使用.NET軟件開發工具包從.NET核心應用

    • 創建Azure的AD用戶解決了「禁」的問題嘗試使用微軟圖表
  • 回答

    1

    要解決的「編譯錯誤創建用戶該型「的IList <>」在未引用的程序定義必須添加到程序集的引用」你應該添加下列到web.config中:

    <assemblies> 
        <add assembly="System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" /> 
    </assemblies> 
    

    這將解決第一個錯誤。

    關於第二個錯誤,請嘗試以確保設置正確的權限在Azure的AD註冊讓你有讀寫權限。

    希望這會有所幫助。

    +0

    謝謝,它幫助,有一個小的修改,因爲我使用project.json –

    +0

    驚人聽聽它與你合作!很高興我幫了忙。 – Mostafa

    1

    發現使用Azure SDK的第一個問題的解決方案, 必須添加依賴項,但在項目的Famework Assemblies部分中。JSON

    "frameworks": { 
        "dnx46": { 
         "dependencies": { 
         "Microsoft.Azure.ActiveDirectory.GraphClient": "2.1.0", 
         "Microsoft.Azure.Common": "2.1.0", 
         "Microsoft.Azure.Management.Resources": "3.4.0-preview", 
         "Microsoft.Azure.Management.Websites": "1.1.0-preview", 
         "Microsoft.Azure.Gallery": "2.6.2-preview", 
         "Microsoft.Azure.Common.Dependencies": "1.0.0", 
         "Microsoft.WindowsAzure.Common": "1.4.1", 
         "Microsoft.WindowsAzure.Management.MediaServices": "4.1.0", 
         "Microsoft.WindowsAzure.Management.Storage": "5.1.1", 
         "Microsoft.WindowsAzure.Management.Compute": "12.3.1", 
         "Microsoft.WindowsAzure.Management.Libraries": "2.0.0", 
         "WindowsAzure.MediaServices": "3.5.2", 
         "windowsazure.mediaservices.extensions": "3.3.0", 
         "Microsoft.IdentityModel.Clients.ActiveDirectory": "3.9.302261508-alpha", 
         "Microsoft.Framework.WebEncoders": "1.0.0-beta8", 
         }, 
         "frameworkAssemblies": { 
         "System.Runtime": "4.0.20.0", 
         "System.Threading.Tasks": "4.0.10.0" 
         } 
        } 
        }, 
    
    3

    如果添加此下依賴於project.json它應該可以解決兼容性問題

    "Microsoft.NETCore.Portable.Compatibility": "1.0.1" 
    
    相關問題