【问题标题】:Microsoft Graph API - Update password - Insufficient privileges to complete the operationMicrosoft Graph API - 更新密码 - 权限不足,无法完成操作
【发布时间】:2024-06-27 19:15:02
【问题描述】:

我正在尝试通过 Microsoft Graph API 更新用户,我能够更新 DisplayNamePasswordProfile 出现错误:

Insufficient privileges to complete the operation.

以下是我在http://jwt.io 解码 JWT 令牌时与令牌关联的角色:

"roles": [
    "User.ReadWrite.All",
    "Directory.ReadWrite.All",
    "Group.ReadWrite.All"
],

基于the documentation,这些权限似乎就足够了。

这是我的代码(取自控制台应用程序),我能够通过 Fiddler 找出调用失败,UpdateAsync 不会引发异常。

try
{
    var userId = "9a5413cd-85ff-4ad1-ab2f-b443941abd8e";
    var token = GetToken().Result;
    System.Console.Write($"Token: {token}");

    var newPassword = "TwDx5zgHxe51DZZ";
    GraphServiceClient graphClient = GetAuthenticatedClient(token);

    // This works -- Updating Display name
    graphClient.Users[userId].Request().UpdateAsync(new User
    {
        DisplayName = "NewDisplayName"
    });

    // This does not work - Updating password
    graphClient.Users[userId].Request().UpdateAsync(new User
    {
        PasswordProfile = new PasswordProfile
        {
            Password = newPassword,
                ForceChangePasswordNextSignIn = true
        }
    });
    System.Console.WriteLine("---Update Complete---");
}
catch (Exception e)
{
    System.Console.WriteLine(e);
}

获取token的方法:

public async Task<string> GetToken()
{
    //  Constants
    var tenant = "dev-mytenantmydomaincom";
    var resource = "https://graph.microsoft.com/";
    var clientID = "XXXXXXXX-87ef-494d-b921-cf8956006b0e";
    var secret = "zgkzas2THJLiD5XXXXXX";

    //  Ceremony
    var authority = $"https://login.microsoftonline.com/{tenant}";
    var authContext = new AuthenticationContext(authority);
    var credentials = new ClientCredential(clientID, secret);
    var authResult = await authContext.AcquireTokenAsync(resource, credentials);
    return authResult.AccessToken;
}

这是 Fiddler 的完整回复:

HTTP/1.1 403 Forbidden
Cache-Control: private
Transfer-Encoding: chunked
Content-Type: application/json
request-id: 6edcf194-7705-4cd7-8144-767925cc9ee4
client-request-id: 6edcf194-7705-4cd7-8144-767925cc9ee4
x-ms-ags-diagnostic: {"ServerInfo":{"DataCenter":"East US","Slice":"SliceB","ScaleUnit":"001","Host":"AGSFE_IN_27","ADSiteName":"EST"}}
Duration: 69.2849
Date: Thu, 31 Aug 2017 13:15:34 GMT

{
    "error": {
        "code": "Authorization_RequestDenied",
            "message": "Insufficient privileges to complete the operation.",
                "innerError": {
            "request-id": "6edcf194-7705-4cd7-8144-767925cc9ee4",
                "date": "2017-08-31T13:15:34"
        }
    }
}    

【问题讨论】:

    标签: microsoft-graph-api


    【解决方案1】:

    密码是一个特别敏感的数据集,因此对它们具有一些独特的权限。来自documentation

    更新passwordProfile 属性时,需要以下范围:Directory.AccessAsUser.All

    Directory.AccessAsUser.All 是需要管理员的委托权限。换句话说,它允许某个全局管理员更改其他用户的passwordProfile

    如果您希望允许最终用户自己更改密码,SDK 中还有一个内置的 ChangePassword 方法:

    await graphClient.Me.ChangePassword("current-pwd, "new-pwd").Request().PostAsync();
    

    注意:这还需要在用户执行之前为 DirectoryAccessAsUser.All 授予管理员同意)

    请记住,DirectoryAccessAsUser.All 是“委托”而不是“应用程序”权限范围。这意味着它仅受 Authorization CodeImplicit 流支持;它不适用于使用Client Credentials 流的守护程序/服务场景。

    如果您考虑到能够随意更改用户密码的非交互式应用程序可能实现的潜在攻击,那么这种限制的原因就很清楚了。

    【讨论】:

    • 感谢您提供这些宝贵的信息。使用客户端凭据通过 Graph API 无法实现这一点是有道理的。
    【解决方案2】:

    我们发现一个简单的解决方案是将应用程序主体添加到“帮助台管理员”角色。

    1. 转到 Azure Active Directory
    2. 在左侧点击角色和管理员
    3. 搜索帮助台管理员角色并点击它

    1. 点击添加分配并粘贴您的应用程序对象ID
    2. 等待 5 分钟左右,让 azure 将更改考虑在内

    【讨论】:

    • Password Administrator 也可能是一个不错的角色选择,因为它提供的权限少于Helpdesk Administrator。文档here.
    • 您好 @BrentRobinson 是的,这是真的,我们发现有几个角色具有必要的权限,因此您可以选择更适合您情况的角色。
    【解决方案3】:

    对于 2021 年参加此问答的任何人 - Graph 中有一个密码重置端点:

    POST /users/{id | userPrincipalName}/authentication/passwordMethods/{id}/resetPassword
    

    您必须先检索密码验证方法的 id。

    此操作所需的权限是UserAuthenticationMethod.ReadWrite.All可以作为应用程序类型权限授予。

    查看文档:https://docs.microsoft.com/en-us/graph/api/passwordauthenticationmethod-resetpassword

    更新

    即使权限可以作为应用程序类型的权限授予,在应用程序上下文中调用也不会成功。 见https://docs.microsoft.com/en-us/answers/questions/246207/34upn-from-claims-with-value-null-is-not-a-valid-u.html

    【讨论】:

      最近更新 更多