gpt4 book ai didi

c# - 如何从 ASP .NET Core 中创建 Azure AD 用户?

转载 作者:行者123 更新时间:2023-12-04 00:10:38 26 4
gpt4 key购买 nike

我目前正在使用 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 创建用户时出现的“禁止”问题

最佳答案

如果将其添加到依赖项下的project.json,它应该可以解决兼容性问题

"Microsoft.NETCore.Portable.Compatibility": "1.0.1"

关于c# - 如何从 ASP .NET Core 中创建 Azure AD 用户?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36220094/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com