关于 azure AD 和图形 API,我有一个具体问题。
我正在向 API 发出一个简单的更改密码请求。这适用于用户能够登录的情况。但是,如果用户通过门户将其密码重置为临时密码,则他们下次登录时需要更改密码。我遇到的问题是在尝试获取该用户的身份验证令牌时返回异常:
AADSTS50055: Force Change Password.
因此,如果我无法获得身份验证令牌,我应该如何使用 Graph API 发送更改密码请求,以便我的用户可以在我的应用程序中更改他们的密码?
下面是我为已经可以登录的用户更改密码的代码:
public SendPasswordChangeRequest(string userId, string newPass, string oldPass) {
try {
HttpClient http = new HttpClient();
string action = string.Format("/users/{0}/changePassword", user.DistinguishedName);
string url = string.Format("https://graph.windows.net/{0}{1}?api-version=1.6", tenantId, action);
object bodyObject = new { currentPassword = oldPass, newPassword = newPass };
string body = JsonConvert.SerializeObject(bodyObject);
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, url);
request.Content = new StringContent(body, Encoding.UTF8, "application/json");
AuthenticationContext authContext = new AuthenticationContext(string.Format("https://login.windows.net/{0}", tenantName));
UserPasswordCredential credential = new UserPasswordCredential(userId, oldPass); //userId and oldPass are parameters passed in
AuthenticationResult authResult = authContext.AcquireTokenAsync("https://graph.windows.net/", clientId, credential).Result;
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", authResult.AccessToken);
HttpResponseMessage httpResponse = http.SendAsync(request).Result;
}
catch (Exception ex) {
//authResult throws the error into here
//Error returned from Azure AD:
//AADSTS50055: Force Change Password.
}
}
简短的回答是:你不能。
这是一个很好的例子,说明为什么在您可以使用任何交互式身份验证流程的任何情况下不鼓励使用用户名/密码流程。来自OAuth 2.0 规范,这是该模式调用的内容(强调):
资源所有者密码凭证(即用户名和密码)可以直接用作授权许可来获取访问令牌。仅当资源所有者和客户端之间存在高度信任(例如,客户端是设备操作系统或高特权应用程序的一部分)并且其他授权授予类型不可用时,才应使用凭据(例如授权码)。
如果您的应用程序是本机客户端应用程序(即富客户端应用程序),您应该调用其中一个AcquireToken
流程,该流程会使用 Azure AD 托管的登录页面弹出登录提示。这将为用户提供更改其密码的机会(并允许其他中断,例如多因素身份验证)。
对于其他方案(Web 应用程序、单页应用程序等),Azure AD 的身份验证方案有更多详细信息,可以在Azure Active Directory 开发人员指南 中找到示例和其他参考。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句