使用 https://graph.microsoft.com 和 ActiveDirectoryClient 将用户添加到 Azure Active Directory

Add User to Azure Active Directory using https://graph.microsoft.com with ActiveDirectoryClient

我正在尝试将用户添加到我的 Azure 租户的 Active Directory。

我正在使用 Microsoft Graph API。通过 Graph Explorer here.

公开的相同内容

问题是无论我通过什么 serviceRoot URI,我都会遇到异常。

我在GetTokenForApplication方法中成功获得了令牌:

ActiveDirectoryClient activeDirectoryClient = new ActiveDirectoryClient(serviceRoot,
                      async () => await GetTokenForApplication());

然而,当我打电话时:

await activeDirectoryClient.Users.AddUserAsync(aadUser);

它抛出这个异常:

 "{\r\n
    \"error\": {\r\n
                   \"code\": \"BadRequest\",\r\n
                   \"message\": \"Query parameter api-version not allowed\",\r\n    
                   \"innerError\": {\r\n
                                    \"request-id\": \"57327a85-8320-4363-b5f9-aeacdf782861\",\r\n
                                    \"date\": \"2019-05-30T21:59:55\"\r\n
                                   }\r\n
               }\r\n
}"

这是我正在使用的 serviceRoot URI:“https://graph.microsoft.com/v1.0

我应该在 serviceRoot 中传递什么 URI?

ActiveDirectoryClientMicrosoft Graph 兼容吗?我问是因为我看到使用 ActiveDirectoryClient 的示例使用的是 Azure AD Graph API.

这个blog post显示了旧Azure AD Graph API和新Microsoft Graph API之间的区别。顺便说一句:Microsoft 建议我们使用 Microsoft Graph API,因为所有新的开发都将集中在它上面。

我认为您正在尝试使用较新的 Microsoft Graph API (https://graph.microsoft.com),但使用较旧的 Azure AD Graph API (https://graph.windows.net) 的客户端库

您可以在此处详细了解比较 - Microsoft Graph or the Azure AD Graph

这是 nuget 包和 class 详细信息:

微软图表 API

  • Microsoft.Graph nuget 包 - 使用 Microsoft Graph API 并使用 GraphServiceClient class.

Azure AD 图 API

  • Microsoft.Azure.ActiveDirectory.GraphClient nuget 包 - 与 Azure AD Graph API 一起使用并使用 ActiveDirectoryClient class。

Microsoft Graph 代码 API 客户端

Microsoft 文档 - Create User - SDK Sample Code

GraphServiceClient graphClient = new GraphServiceClient( authProvider );

var user = new User
{
    AccountEnabled = true,
    DisplayName = "displayName-value",
    MailNickname = "mailNickname-value",
    UserPrincipalName = "upn-value@tenant-value.onmicrosoft.com",
    PasswordProfile = new PasswordProfile
    {
        ForceChangePasswordNextSignIn = true,
        Password = "password-value"
    }
};

await graphClient.Users
    .Request()
    .AddAsync(user);