Microsoft.Graph.GraphServiceClient 允许我创建用户但不允许我更改密码

时间:2021-01-20 02:52:31

标签: microsoft-graph-api azure-ad-b2c

我有一个由 Azure 中托管的 Angular SPA 和一些用于 API 的 Azure 函数组成的系统。在一个管理应用程序中,我创建了一个应用程序,允许管理员用户创建新的用户帐户,包括指定密码。这些新帐户也可以登录我创建的业务线应用程序。有一个要求,我们需要允许创建帐户的同一个人重置密码。出于某种原因,我编写的用于设置密码的代码不起作用。用户可以创建帐户,包括设置密码,这似乎很奇怪,但由于某种原因,同一用户不能独立于创建用户帐户来设置密码。仅供参考,没有电子邮件,这些是用户帐户,因此无法请求重置密码。

这里是错误:

{
  "error": {
    "code": "Authorization_RequestDenied",
    "message": "Access to change password operation is denied.",
    "innerError": {
      "date": "2021-01-19T21:58:35",
      "request-id": "a1bc5b50-83e9-47ae-97c7-bda4f524fa0e",
      "client-request-id": "a1bc5b50-83e9-47ae-97c7-bda4f524fa0e"
    }
  }
}

这是我的代码:

//this is the method that works
public async Task<Microsoft.Graph.User> CreateUserAsync(string givenName,
    string surname, string displayName, string userPrincipalName, string issuer, 
    string signInType, string initialPassword, GraphServiceClient graphClient) 
{
    var user = new Microsoft.Graph.User {
        AccountEnabled = true,
        GivenName = givenName,
        Surname = surname,
        DisplayName = displayName,
        Identities = new List<ObjectIdentity>() {
            new ObjectIdentity {
                Issuer = issuer, 
                IssuerAssignedId = userPrincipalName,
                SignInType = signInType
            }
        },
        PasswordProfile = new PasswordProfile {
            ForceChangePasswordNextSignIn = false,
            Password = initialPassword
        }
    };

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

//This one does not work, returns: Access to change password operation is denied.
public async Task<Microsoft.Graph.User> SetPasswordAsync(
string userName, string currentPassword, string newPassword, GraphServiceClient graphClient) 
{
    await graphClient.Users[userName].ChangePassword(currentPassword, newPassword).Request().PostAsync();

    return something here;
}

1 个答案:

答案 0 :(得分:0)

这里似乎是一个权限问题。函数:graphClient.Users[].ChangePassword()基于reset password rest API,正如官方文档所指出的,这里只有委托权限有效,并且需要权限:UserAuthenticationMethod.ReadWrite.All。 在我向我的应用授予此权限后: enter image description here

它非常适合我: enter image description here

如果您有任何其他问题,请告诉我。