C# 如何从ASP.NETCore中创建Azure广告用户?
我目前正在使用ASP.NETCore在门户网站中工作。 其中一个要求是创建Azure广告用户,在创建过程中发现了几个问题 首先,在尝试使用GraphClient SDK时,我遇到以下编译错误:C# 如何从ASP.NETCore中创建Azure广告用户?,c#,asp.net,asp.net-mvc,azure,asp.net-core,C#,Asp.net,Asp.net Mvc,Azure,Asp.net Core,我目前正在使用ASP.NETCore在门户网站中工作。 其中一个要求是创建Azure广告用户,在创建过程中发现了几个问题 首先,在尝试使用GraphClient SDK时,我遇到以下编译错误: Severity Code Description Project File Line Suppression State Error CS0012 The type 'IList<>' is defined in an assembly that is not
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
这是在尝试实例化ActiveDirectoryClient时发生的
ActiveDirectoryClient adClient=新的ActiveDirectoryClient(serviceRoot,null)
我认为这两个错误是由于SDK尚未与.NETCore完全兼容,因为已经存在Uri类型
我已经在用它了,它是一个不同的版本
//由.NET Reflector从C:\Windows\Microsoft.NET\assembly\GAC_MSIL\System\v4.0_4.0.0.0_b77a5c561934e089\System.dll生成
我在这上面花了太多的时间,所以我决定尝试使用MicrosoftGraph,但我仍然得到一个“禁止”的响应
在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;
}
public静态只读字符串CreateUserUrl=@”https://graph.microsoft.com/{0}/用户”;
公共静态异步任务CreateUser(字符串accessToken、UserInfo pUser)
{
使用(var client=new HttpClient())
{
使用(var request=newhttprequestmessage(HttpMethod.Post,Settings.CreateUserUrl.v10Version())
{
request.Headers.Accept.Add(Json);
request.Headers.Authorization=新的AuthenticationHeaderValue(“承载者”,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=newstringcontent(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
使用(var-response=await-client.sendsync(请求))
{
if(response.StatusCode==HttpStatusCode.OK)
{
var json=JObject.Parse(wait response.Content.ReadAsStringAsync());
//myInfo.DisplayName=json?[“DisplayName”]?.ToString();
//myInfo.MailAddress=json?[“邮件”]?.ToString().Trim().Replace(“,string.Empty”);
//myInfo.Department=json?[“Department”]?.ToString();
//myInfo.PhotoBytes=await GetUserPhotoAsync(accessToken,json?[“userPrincipalName”]?.ToString());
}
}
}
}
返回pUser;
}
注意:我已经可以作为Azure广告用户登录,并且我还可以使用Microsoft Graph获取信息
有没有办法解决这两个问题中的任何一个
- 从.NET核心应用程序中使用.NET SDK创建Azure广告用户
- 解决尝试使用Microsoft Graph创建用户时遇到的“禁止”问题
<assemblies>
<add assembly="System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
</assemblies>
这将解决第一个错误
关于第二个错误,请确保在Azure AD注册中设置正确的权限,以便您具有读写权限
希望这有帮助。使用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"
}
}
},
如果将其添加到依赖项下的project.json中,则应修复兼容性问题
"Microsoft.NETCore.Portable.Compatibility": "1.0.1"
谢谢,这很有帮助,因为我使用的是project.json,所以做了一些小修改
"Microsoft.NETCore.Portable.Compatibility": "1.0.1"