使用图形API更改密码(Azure AD B2C)

使用图形API更改密码(Azure AD B2C),azure,azure-ad-b2c,Azure,Azure Ad B2c,从angular前端和webapi作为后端,我尝试使用Graph API change password函数,但我得到以下错误: {“odata.error”:{“code”:“Authorization_RequestDenied”,“message”:{“lang”:“en”,“value”:“更改密码操作的访问被拒绝”。}}}} 下面是我的代码: private async void ChangePasswordPostRequest(ChangePasswordMo

从angular前端和webapi作为后端,我尝试使用Graph API change password函数,但我得到以下错误:

{“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);
                }
            }
我被困在这里,任何帮助都将不胜感激。提前感谢。

只能代表已登录用户进行呼叫

应用程序可以使用更改用户的密码

必须使用该应用程序才能更改用户的密码

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

有了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}"
}

如果使用客户端凭据,IIRC密码重置要求应用程序具有相当大的权限(就像您正在做的那样)。您为您的应用程序授予了哪些应用程序权限?您也尝试过MS Graph API吗:?谢谢@juunas的回复。我已经在AD B2C中授予了所有委托和应用程序权限,我没有尝试MS Graph API,因为所有用户管理操作都是使用Graph API完成的。对于更改密码,我遵循以下链接:如果您仅使用客户端id和密码(看起来是这样)拨打电话,则需要应用程序权限。我已交叉检查并发现所有应用程序权限都已分配给该应用程序。但我如何确定尝试更改其密码的用户是否已登录?我已经在B2C中为应用程序中的应用程序授予了签名用户权限。Hi@RajGupta我建议您阅读解释web API如何识别已登录用户的文章。Hi@Chris Padgett,我成功地调用了changePassword。参考授权码流,首先获取授权码,然后获取访问令牌。但现在我面临另一个问题,我可以用用户名更改用户的密码xyz@myb2cname.onmicrosoft.com但不适用于具有不同域的其他用户,例如:test@gmail.com(但仍然是b2c用户)当我尝试为这些用户获取访问令牌时,我得到以下错误:{“错误描述”:“AADSTS50034:要登录此应用程序,必须将帐户添加到myb2cname.onmicrosoft.com目录}Hi@RajKumarGupta请参阅我对的最新答复。