【问题标题】:Why do I encounter "Authorization_RequestDenied: Insufficient privileges to complete the operation" when resetting a password via Graph API为什么我在通过 Graph API 重置密码时会遇到“Authorization_RequestDenied: Insufficient rights to complete the operation”
【发布时间】:2020-10-17 11:23:07
【问题描述】:

我已经克隆了 MS Identity DotNetCore B2C Account Management 代码示例并为我的 B2C 租户配置了它。 get user 和 delete user 命令都可以工作。但是,当我尝试使用命令 [5] Update user password 时遇到此错误:

 Code: Authorization_RequestDenied
 Message: Insufficient privileges to complete the operation.

this Microsoft support issue for Office 365 的描述似乎与我遇到的重叠。这意味着管理员帐户的级别高于应用程序可以触及的级别,因为该应用程序不是 AD 中的管理员。

那么,应用程序可以管理普通的非管理员用户,但不能管理管理员帐户?我理解正确吗。如果是这样,有没有办法提升我注册的应用程序的权限,以便更新管理员和非管理员帐户的密码?

对示例的唯一更改是在 appsettings.json 中,并且需要对 "TenantId""AppId""ClientSecret" 的值进行预期的自定义。

我已经注册了一个应用程序并为 Graph API 授予了以下权限:

  • AuditLog.ReadAll
  • Directory.ReadWriteAll
  • UserManageIdentities.All
  • User.ReadWrite.All

这是尝试更新用户密码的代码:

        public static async Task SetPasswordByUserId(GraphServiceClient graphClient)
        {
            Console.Write("Enter user object ID: ");
            string userId = Console.ReadLine();
            Console.Write("Enter new password: ");
            string password = Console.ReadLine();

            Console.WriteLine($"Looking for user with object ID '{userId}'...");

            var user = new User
            {
                PasswordPolicies = "DisablePasswordExpiration,DisableStrongPassword",
                PasswordProfile = new PasswordProfile
                {
                    ForceChangePasswordNextSignIn = false,
                    Password = password,
                }
            };

            try
            {
                // Update user by object ID
                await graphClient.Users[userId]
                   .Request()
                   .UpdateAsync(user);

                Console.WriteLine($"User with object ID '{userId}' successfully updated.");
            }
            catch (Exception ex)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine(ex.Message);
                Console.ResetColor();
            }
        }

【问题讨论】:

    标签: c# azure .net-core microsoft-graph-api azure-ad-b2c


    【解决方案1】:

    要更新用户的passwordProfile,需要Directory.AccessAsUser.All权限。

    doc

    您应该注意Directory.AccessAsUser.AllDelegated permission,而不是Application permission。这意味着权限不会在client credential flow生效(你提供的sample使用它),所以当你使用Microsoft Graph SDK时,你不能使用Client credentials provider,你的选择是使用@987654326 @(推荐)。

    IConfidentialClientApplication confidentialClientApplication = ConfidentialClientApplicationBuilder
        .Create(clientId)
        .WithRedirectUri(redirectUri)
        .WithClientSecret(clientSecret) // or .WithCertificate(certificate)
        .Build();
    
    AuthorizationCodeProvider authProvider = new AuthorizationCodeProvider(confidentialClientApplication, scopes);
    

    【讨论】:

    • 我有点困惑的是为什么委派权限会起作用。我从围绕示例阅读的所有文档中得到的印象是它只涉及应用程序 API 权限
    • @TrevorReid 是的,API 权限有两种,Application 权限和 Delegated 权限,在这种情况下,您需要 Directory.AccessAsUser.All,它只是存在于 MS Graph 的 Delegated 权限中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多