【问题标题】:Change password using Graph API (Azure AD B2C)使用 Graph API (Azure AD B2C) 更改密码
【发布时间】:2018-12-19 10:51:14
【问题描述】:

从 Angular 前端和 webapi 作为后端,我正在尝试使用 Graph API 更改密码功能,但出现以下错误:

{"odata.error":{"code":"Authorization_RequestDenied","message":{"lang":"en","value":"访问更改密码操作被拒绝。"} }}

下面是我的代码:

           private async void ChangePasswordPostRequest(ChangePasswordModel changePasswordModel){
                AuthenticationResult result = await authContext.AcquireTokenAsync(ApplicationConstants.aadGraphResourceId, credential);
                HttpClient http = new HttpClient();
                string url = ApplicationConstants.aadGraphEndpoint + tenant + "/users/" + "c55f7d4d-f81d-4338-bec7-145225366565" + "/changePassword?" + ApplicationConstants.aadGraphVersion;         

                HttpRequestMessage request = new HttpRequestMessage(new HttpMethod("POST"), url);
                request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", result.AccessToken);

                request.Content = new StringContent(JsonConvert.SerializeObject(new ChangePasswordPostModel() { currentPassword = changePasswordModel.CurrentPassword, newPassword = changePasswordModel.NewPassword }), Encoding.UTF8, "application/json");

                HttpResponseMessage response = await http.SendAsync(request);
                if (!response.IsSuccessStatusCode)
                {
                    string error = await response.Content.ReadAsStringAsync();
                    object formatted = JsonConvert.DeserializeObject(error);
                }
            }

我坚持这一点,任何帮助将不胜感激。提前致谢。

【问题讨论】:

  • IIRC 密码重置需要应用程序的大量权限,如果使用客户端凭据(正如您似乎正在做的那样)。您为您的应用授予了哪些应用权限?您是否也尝试过 MS Graph API:developer.microsoft.com/en-us/graph/docs/api-reference/v1.0/api/…
  • 感谢@juunas 的回复。我已经在 AD B2C 中授予了所有委派和应用程序权限,但我没有尝试 MS Graph API,因为所有用户管理操作都是使用 Graph API 完成的。为了更改密码,我点击了这个链接:msdn.microsoft.com/en-us/library/azure/ad/graph/api/…
  • 如果您只使用客户端 ID 和密码(看起来像您一样)进行呼叫,则需要 应用程序权限
  • 我已经交叉检查,发现所有应用程序权限都分配给了应用程序。

标签: azure azure-ad-b2c


【解决方案1】:

修改密码操作只能代调用 登录用户。应用程序可以更改用户的密码 使用重置密码操作。必须分配应用程序 以用户帐户管理员角色更改密码 用户。 @Chris Padgett

使用 Graph API 的 beta 端点,现在可以在没有 PowerShell 的情况下完成它!

//Get App ObjectId
https://graph.microsoft.com/beta/servicePrincipals?$filter=appId eq '{appId}'

//Get roleId User Account Administrator role
GET: https://graph.microsoft.com/v1.0/directoryRoles?$filter=roleTemplateId eq 'fe930be7-5e62-47db-91af-98c3a49a38b1'

//If not found //Activate
POST: https://graph.microsoft.com/v1.0/directoryRoles

{
  "displayName": "User Account Administrator",
  "roleTemplateId": "fe930be7-5e62-47db-91af-98c3a49a38b1"
}

//Add member
POST: https://graph.microsoft.com/beta/directoryRoles/{Id}/members/$ref
{
  "@odata.id": "https://graph.microsoft.com/beta/servicePrincipals/{Id returned in first request}"
}

【讨论】:

    【解决方案2】:

    The change password operation 只能代表登录用户调用。

    应用程序可以使用the reset password operation 更改用户的密码。

    应用程序必须是assigned to the user account administrator role 才能更改用户的密码。

    【讨论】:

    • 但是我如何确定尝试更改密码的用户是否已登录?我已在 B2C 中的应用程序权限中授予应用程序的用户权限。
    • 嗨@RajGupta,我建议您阅读the "Azure AD B2C: Single-page app sign-in by using OAuth 2.0 implicit flow" article,它解释了Web API 如何识别登录用户。
    • 嗨! @Chris Padgett,我能够成功调用 changePassword。参考授权码流程先获取授权码再获取access_token。但现在我面临另一个问题,我可以更改用户名 xyz@myb2cname.onmicrosoft.com 不适合 其他具有不同域的用户的密码,例如:test@gmail.com(但仍然是 b2c 用户)当我尝试为这些用户获取 access_token 时,我收到以下错误:{“error_description”:“AADSTS50034:要登录此应用程序帐户必须添加到 myb2cname.onmicrosoft.com 目录}
    • 嗨@RajKumarGupta 请参考我对your newest question的最新答复。
    猜你喜欢
    • 2018-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-02
    • 1970-01-01
    • 1970-01-01
    • 2019-08-10
    • 1970-01-01
    相关资源
    最近更新 更多