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

时间:2018-07-11 11:48:47

标签: azure azure-ad-b2c

从有角前端和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);
                }
            }

我对此一无所知,我们将不胜感激。预先感谢。

2 个答案:

答案 0 :(得分:0)

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

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

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

答案 1 :(得分:0)

  

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

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

models/Dropdowns_m.php