【问题标题】:How to create Azure AD users from within ASP .NET Core?如何从 ASP .NET Core 中创建 Azure AD 用户?
【发布时间】: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


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      要修复“类型 'IList' 在未引用的程序集中定义的编译错误。您必须添加对程序集的引用”,您应该在 web.config 中添加以下内容:

      <assemblies>
          <add assembly="System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
      </assemblies>
      

      这将解决第一个错误。

      关于第二个错误,请尝试确保在您的 Azure AD 注册中设置正确的权限,以便您拥有读取和写入权限。

      希望这会有所帮助。

      【讨论】:

      • 谢谢,它有帮助,因为我使用的是 project.json,所以做了一些小修改
      【解决方案3】:

      使用 Azure SDK 找到了第一个问题的解决方案, 必须添加依赖项,但在 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"
            }
          }
        },
      

      【讨论】:

        猜你喜欢
        • 2021-10-31
        • 1970-01-01
        • 2019-03-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-08-10
        • 1970-01-01
        相关资源
        最近更新 更多