【发布时间】:2016-07-13 05:14:47
【问题描述】:
我目前正在使用 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 = new 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 = new ActiveDirectoryClient(serviceRoot, null);
我认为这两个错误是由于 SDK 尚未与 .NET Core 完全兼容,因为已经存在 Uri 类型 我已经在使用的是不同的版本
// 由 .NET Reflector 从 C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System\v4.0_4.0.0.0__b77a5c561934e089\System.dll 生成
我在这方面花费了太多时间,所以我决定尝试使用 Microsoft Graph,但我不断收到“禁止”响应 在 Azure AD 中将读取和写入目录数据添加到应用程序后 这是当前的代码
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 Graph 获取信息。
有什么想法可以解决这两个问题中的任何一个吗?
- 在 .NET Core 应用中使用 .NET SDK 创建 Azure AD 用户
- 解决尝试使用 Microsoft Graph 创建用户的“禁止”问题
【问题讨论】:
标签: c# asp.net asp.net-mvc azure asp.net-core