【问题标题】:Resetting a user's password using Microsoft Graph使用 Microsoft Graph 重置用户密码
【发布时间】:2018-04-16 06:50:28
【问题描述】:

我正在尝试编写一个 Web 门户,用户可以使用它来重置他们自己的 Azure AD 密码。由于我的客户的要求,Azure AD SSPR 不是一个选项

为了实现这一点,我正在使用 Microsoft Graph。根据the documentation,如果您拥有User.ReadWrite.AllDirectory.AccessAsUser.All 权限,则可以使用Microsoft Graph 重置用户密码。

然后是permissions documentation,它声明即使您拥有Directory.ReadWrite.All 权限,您也无法重置用户密码。

我已经进行了测试,看看这是否可行,但我收到了 HTTP 403 Forbidden 响应。

我使用的代码是:

string ResourceUrl = "https://graph.windows.net/";
string AuthorityUrl = "https://login.microsoftonline.com/companyxxx.onmicrosoft.com/oauth2/authorize/";

//Create a user password cradentials.
var credential = new Microsoft.IdentityModel
    .Clients
    .ActiveDirectory
    .UserPasswordCredential("username@xxxx.com", "passwordxxx");

// Authenticate using created credentials
var authenticationContext = new AuthenticationContext(AuthorityUrl);

var authenticationResult = authenticationContext
    .AcquireTokenAsync(ResourceUrl, "xxxxxxxx-3017-4833-9923-30d05726b32f", credential)
    .Result;

string jwtToken = authenticationResult.AccessToken;
var cred = new Microsoft.Rest
    .TokenCredentials(authenticationResult.AccessToken, "Bearer");

HttpClient client = new HttpClient();
var queryString = HttpUtility.ParseQueryString(string.Empty);
queryString["api-version"] = "1.6";
client.DefaultRequestHeaders
    .Accept
    .Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", jwtToken);

var uri = "https://graph.windows.net/xxxxxxxx-18fe-xxxx-bb90-d62195600495/users/xxxxxxxx-aa58-4329-xxxx-b39af07325ee?" + queryString;

//var content = new StringContent("{\"passwordProfile\": {\"password\": \"Test123456\", \"forceChangePasswordNextLogin\": true }}");
var response = client.PatchAsync(new Uri(uri), content, jwtToken);

PatchAsync 方法是一个扩展方法,如下:

public static class HttpClientExtensions
{
    public static async Task<HttpResponseMessage> PatchAsync(this HttpClient client,
        Uri requestUri, HttpContent iContent, string jwtToken)
    {
        var method = new HttpMethod("PATCH");
        var request = new HttpRequestMessage(method, requestUri)
        {
            Content = iContent,
        };
        request.Content.Headers.ContentType =
            new MediaTypeHeaderValue("application/json");

        request.Headers.Authorization =
            new AuthenticationHeaderValue("Bearer", jwtToken);

        HttpResponseMessage response = new HttpResponseMessage();
        try
        {
            response = await client.SendAsync(request);
        }
        catch (TaskCanceledException e)
        {
            Console.WriteLine("ERROR: " + e.ToString());
        }

        return response;
    }
}

有人可以澄清一下这是否可以使用带有用户名和密码的凭据授予流程进行身份验证。如果是这样,我该如何实现?

【问题讨论】:

  • 您是否检查过 JWT 是否包含必要的范围?您可以使用jwt.ms 之类的工具来解码令牌。

标签: c# azure-active-directory microsoft-graph-api


【解决方案1】:

您正在混淆 Microsoft Graph 和 Azure AD Graph API。这是两个不同的 API,对其中一个的调用不能与另一个互换。

您是正确的,您需要为此活动使用Directory.AccessAsUser.All 范围。此范围允许 API 对 AAD 执行登录用户自己能够执行的任何操作(即更改他们自己的密码)。

一旦拥有Directory.AccessAsUser.All 权限的用户的有效access_token,您就可以更新用户的passwordProfile

PATCH https://graph.microsoft.com/v1.0/me
Content-type: application/json

{
  "passwordProfile" : {
    "forceChangePasswordNextSignIn": true,
    "password": "password-value"
  }
}

【讨论】:

  • 好的,但我的想法是我在 Azure AD 上创建一个具有正确权限的用户,然后使用该用户重置来宾用户的密码。我将如何使用 Microsoft Graph API 实现这一目标?
  • 无法更改 Azure AD 中 guest 用户的密码。这就是访客帐户的想法——他们管理自己的密码(在理想情况下,这些密码已经由自己的 IT 部门管理),而您只管理他们对业务关键应用程序的访问。
猜你喜欢
  • 2018-08-29
  • 1970-01-01
  • 1970-01-01
  • 2019-02-18
  • 2021-05-25
  • 1970-01-01
  • 1970-01-01
  • 2018-02-01
  • 1970-01-01
相关资源
最近更新 更多