【问题标题】:Insufficient privileges to add Azure AD user添加 Azure AD 用户的权限不足
【发布时间】:2020-07-22 19:49:48
【问题描述】:

我创建了一个控制台应用程序来创建 Azure AD 用户,如下所示(文档引用:https://docs.microsoft.com/en-us/graph/api/user-post-users?view=graph-rest-1.0&tabs=http):

static async Task Main(string[] args)
        {
            var credential = new ClientCredential("<clientt-id>", "<client-seceret>");
            var authProvider = new HttpRequestMessageAuthenticationProvider(
                                                        credential,
                                                        "https://login.windows.net/<tenant-id>",
                                                        "https://graph.microsoft.com/");

            GraphServiceClient graphClient = new GraphServiceClient(authProvider);

            var user = new User
            {
                AccountEnabled = true,
                DisplayName = "Test User",
                MailNickname = "testuser",
                UserPrincipalName = "testuser@M365xxxxxxx.onmicrosoft.com ",
                PasswordProfile = "xxxxxxxxxxxx"
                OnPremisesImmutableId = "id"
            };

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

添加到应用的 API 权限是 Group.ReadWrite.All 和 User.ReadWrite.All。

在运行此代码时,我看到以下错误:

代码:Authorization_RequestDenied 消息:权限不足,无法完成操作。

我错过了什么?

【问题讨论】:

  • API 权限是否已获得管理员同意?
  • 是授予权限
  • 您好,请问您的问题解决了吗?如果还有问题,请告诉我。

标签: c# azure azure-active-directory


【解决方案1】:

对于这个问题,我总结了以下几点,你需要检查:

1.您的代码似乎使用 client_credentials 作为授权流程来完成这项工作,因此请检查您是否添加了“应用程序”权限而不是“委托”权限​​。并且不要忘记授予管理员同意。

2. 如果仍然显示Authorization_RequestDenied 消息,请移除Group.ReadWrite.All 权限,因为该权限是不必要的。而Group权限可能会影响我过去测试中的其他权限。

3. 貌似你在HttpRequestMessageAuthenticationProvider类中开发了具体代码,其实有现成的SDK可供我们使用。我在下面提供了我的代码供您参考,该代码可以正常创建用户。

using Microsoft.Graph;
using Microsoft.Graph.Auth;
using Microsoft.Identity.Client;
using System;
using System.Threading.Tasks;

namespace ConsoleApp23
{
    class Program
    {
        static async Task Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            IConfidentialClientApplication confidentialClientApplication = ConfidentialClientApplicationBuilder
                .Create("<client_id>")
                .WithTenantId("<tenant_id>")
                .WithClientSecret("<client_secret>")
                .Build();

            ClientCredentialProvider authProvider = new ClientCredentialProvider(confidentialClientApplication);

            GraphServiceClient graphClient = new GraphServiceClient(authProvider);

            var user = new User
            {
                AccountEnabled = true,
                DisplayName = "huryAdd",
                MailNickname = "huryAdd",
                UserPrincipalName = "huryAdd@xxx.onmicrosoft.com",
                PasswordProfile = new PasswordProfile
                {
                    ForceChangePasswordNextSignIn = true,
                    Password = "Password0123"
                },
                OnPremisesImmutableId = "testOnPre"
            };

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

            Console.WriteLine("====success====");
        }
    }
}

并且还提供我项目中安装的包。

Install-Package Microsoft.Identity.Client -Version 4.16.1
Install-Package Microsoft.Graph
Install-Package Microsoft.Graph.Auth -IncludePrerelease

4.顺便说一句,您的UserPrincipalName 末尾有一个空格。请删除它,否则它会显示无效的主体名称。

希望对你有帮助~

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多