我目前正在使用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
尝试设置对象newUser.OtherMails = new System.Collections.Generic.List();的OtherMails属性时发生这种情况。
另一个编译错误是
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获取信息。
有什么想法可以解决两个问题中的任何一个吗?
使用Azure SDK找到了第一个问题的解决方案,不得不添加dependenci,但是在project.json的Famework Assemblies部分中
"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"
}
}
},
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句