如何在 Azure AD 用户中重置密码?
How to reset password in Azure AD user?
我正在使用 azure AD 进行应用程序身份验证。已在 Azure AD 中成功创建用户。用户可以使用密码登录。我的要求是用户如何重置自己的密码。当用户忘记密码时,他们如何在我的应用程序中重置自己的密码。有没有可用的图表api?
Resetting a user's password is a special case of the update user operation. Specify the passwordProfile property for the User. The request contains a valid PasswordProfile
object that specifies a password that satisfies the tenant’s password complexity policy. The password policy typically imposes constraints on the complexity, length, and re-use of a password. For more information, see the PasswordProfile 主题。
您可以通过修补用户对象来重置用户密码:
PATCH https://graph.windows.net/myorganization/users/{user_id}?api-version=1.6
{
"passwordProfile": {
"password": "{password}",
"forceChangePasswordNextLogin": false
},
"passwordPolicies": "DisablePasswordExpiration"
}
准备:
1.Switch 您在 Azure AD 中拥有管理员 authority.Add 新用户 的目录。获取用户名和密码。
注意:设置用户名时,@后面是你的整个目录名。首次登录时,需要修改密码。
2.Go到你注册的Native App,添加权限以登录用户身份访问目录到应用程序。
注意:需要委派范围 User.ReadWrite.All
或 Directory.AccessAsUser.All
才能重置用户密码。除了正确的范围外,signed-in
用户还需要足够的权限才能重置其他用户的密码。
3.Now,您可以参考下面的代码:
var graphResourceId = "https://graph.windows.net/";
var tenantId = "xxxxxxxxxxxxxxxxxxxxx";
var clientId = "xxxxxxxxxxxxxxxxxxxxxxx";
var username = "xxxxxxxxxxxxxxxxxxxx";
var password = "xxxxxxxxx";
var servicePointUri = new Uri(graphResourceId);
var serviceRoot = new Uri(servicePointUri, tenantId);
string aadInstance = "https://login.microsoftonline.com/" + tenantId + "/oauth2/token";
AuthenticationContext authenticationContext = new AuthenticationContext(aadInstance, false);
UserPasswordCredential credential = new UserPasswordCredential(username, password);
AuthenticationResult authenticationResult = authenticationContext.AcquireTokenAsync(graphResourceId, clientId, credential).Result;
var accessToken = authenticationResult.AccessToken;
HttpClient http = new HttpClient();
string url = "https://graph.windows.net/" + tenantId + "/users/" + username + "?api-version=1.6";
var method = new HttpMethod("PATCH");
HttpRequestMessage request = new HttpRequestMessage(method, url);
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", authenticationResult.AccessToken);
var body = "{\"passwordProfile\": {\"password\": \"YourNewPassword\",\"forceChangePasswordNextLogin\": false},\"passwordPolicies\":\"DisablePasswordExpiration\"}";
request.Content = new StringContent(body, Encoding.UTF8, "application/json");
HttpResponseMessage response = http.SendAsync(request).Result;
4.Here 是输出:
我正在使用 azure AD 进行应用程序身份验证。已在 Azure AD 中成功创建用户。用户可以使用密码登录。我的要求是用户如何重置自己的密码。当用户忘记密码时,他们如何在我的应用程序中重置自己的密码。有没有可用的图表api?
Resetting a user's password is a special case of the update user operation. Specify the passwordProfile property for the User. The request contains a valid PasswordProfile
object that specifies a password that satisfies the tenant’s password complexity policy. The password policy typically imposes constraints on the complexity, length, and re-use of a password. For more information, see the PasswordProfile 主题。
您可以通过修补用户对象来重置用户密码:
PATCH https://graph.windows.net/myorganization/users/{user_id}?api-version=1.6
{
"passwordProfile": {
"password": "{password}",
"forceChangePasswordNextLogin": false
},
"passwordPolicies": "DisablePasswordExpiration"
}
准备:
1.Switch 您在 Azure AD 中拥有管理员 authority.Add 新用户 的目录。获取用户名和密码。
注意:设置用户名时,@后面是你的整个目录名。首次登录时,需要修改密码。
2.Go到你注册的Native App,添加权限以登录用户身份访问目录到应用程序。
User.ReadWrite.All
或 Directory.AccessAsUser.All
才能重置用户密码。除了正确的范围外,signed-in
用户还需要足够的权限才能重置其他用户的密码。
3.Now,您可以参考下面的代码:
var graphResourceId = "https://graph.windows.net/";
var tenantId = "xxxxxxxxxxxxxxxxxxxxx";
var clientId = "xxxxxxxxxxxxxxxxxxxxxxx";
var username = "xxxxxxxxxxxxxxxxxxxx";
var password = "xxxxxxxxx";
var servicePointUri = new Uri(graphResourceId);
var serviceRoot = new Uri(servicePointUri, tenantId);
string aadInstance = "https://login.microsoftonline.com/" + tenantId + "/oauth2/token";
AuthenticationContext authenticationContext = new AuthenticationContext(aadInstance, false);
UserPasswordCredential credential = new UserPasswordCredential(username, password);
AuthenticationResult authenticationResult = authenticationContext.AcquireTokenAsync(graphResourceId, clientId, credential).Result;
var accessToken = authenticationResult.AccessToken;
HttpClient http = new HttpClient();
string url = "https://graph.windows.net/" + tenantId + "/users/" + username + "?api-version=1.6";
var method = new HttpMethod("PATCH");
HttpRequestMessage request = new HttpRequestMessage(method, url);
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", authenticationResult.AccessToken);
var body = "{\"passwordProfile\": {\"password\": \"YourNewPassword\",\"forceChangePasswordNextLogin\": false},\"passwordPolicies\":\"DisablePasswordExpiration\"}";
request.Content = new StringContent(body, Encoding.UTF8, "application/json");
HttpResponseMessage response = http.SendAsync(request).Result;
4.Here 是输出: