简体   繁体   English

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

[英]Change password using Graph API (Azure AD B2C)

From angular front-end and webapi as back-end, I'm trying to consume Graph API change password function, but I get following error: 从有角前端和webapi作为后端,我试图使用Graph API更改密码功能,但出现以下错误:

{"odata.error":{"code":"Authorization_RequestDenied","message":{"lang":"en","value":"Access to change password operation is denied."}}} {“ odata.error”:{“ code”:“ Authorization_RequestDenied”,“ message”:{“ lang”:“ en”,“ value”:“拒绝更改密码操作。”}}}

Below is my code: 下面是我的代码:

           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);
                }
            }

I'm stuck at this, any help will be appreciated. 我对此感到困惑,将不胜感激。 Thanks in advance. 提前致谢。

The change password operation can only be called on behalf of the signed-in user. 只能代表登录用户调用更改密码操作

An application can change the password for a user using the reset password operation . 应用程序可以使用“重置密码”操作来更改用户的密码

The application must be assigned to the user account administrator role to change the password for the user. 必须将应用程序分配给用户帐户管理员角色,才能更改用户密码。

The change password operation can only be called on behalf of the signed-in user. 只能代表登录用户调用更改密码操作。 An application can change the password for a user using the reset password operation. 应用程序可以使用重置密码操作来更改用户的密码。 The application must be assigned to the user account administrator role to change the password for the user. 必须将应用程序分配给用户帐户管理员角色,才能更改用户密码。 @Chris Padgett @克里斯·帕吉特

With the beta endpoint of the Graph API it's now possible to get it done without PowerShell! 使用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}"
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM