如何使用Azure Active Directory B2C GraphServiceClient创建全局管理员用户?

时间:2018-02-15 15:22:06

标签: c# asp.net-mvc microsoft-graph azure-ad-b2c

我想使用GraphServiceClient(Microsoft Graph API,而不是Azure AD Graph API)在Azure Active Directory B2C中创建新用户。我通过设置UserType = "Global Administrator"尝试了以下操作,但这不起作用,newAdminUser显示为常规User而不是Global Administrator。我做错了什么?

User newAdminUser = await graphClient.Users.Request().AddAsync(new User
{
    AccountEnabled = true,
    DisplayName = userBinding.FullName,
    MailNickname = userBinding.UserName,
    UserPrincipalName = userBinding.UserName + '@' + domain,
    PasswordProfile = new PasswordProfile
    {
        Password = new NetworkCredential(string.Empty, userBinding.Password).Password,
        ForceChangePasswordNextSignIn = true
    }
});

await graphClient.Users[newAdminUser.Id].Request().UpdateAsync(new User()
{
    UserType = "Global Administrator"
});

以下是newAdminUser Azure Active Directory B2C配置文件的屏幕截图,显示授予的权限不是Global Administratorenter image description here

1 个答案:

答案 0 :(得分:3)

所有用户(无论是否为管理员)都是目录中的user对象。给定user的访问权限取决于它们所属的目录角色。

为了将某人分配给"全球管理员"在角色中,您需要将user添加到正确的角色。

  1. 通过检索list of directoryRoles
  2. 找到所需角色的id
    var directoryRoles = await graphClient.DirectoryRoles
        .Request()
        .GetAsync();
    
    1. Add the user角色:
    2. await graphClient.DirectoryRoles["role-id"]
          .Members
          .References
          .Request()
          .AddAsync(newAdminUser);