我目前在使用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用戶解決了「禁」的問題嘗試使用微軟圖表
謝謝,它幫助,有一個小的修改,因爲我使用project.json –
驚人聽聽它與你合作!很高興我幫了忙。 – Mostafa