c# - 使用 Microsoft Graph 重置用户密码

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

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

为了实现这一目标,我使用 Microsoft Graph。根据 the documentation ,如果您拥有 User.ReadWrite.All 或 Directory.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("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b3c6c0d6c1ddd2ded6f3cbcbcbcb9dd0dcde" rel="noreferrer noopener nofollow">[email protected]</a>", "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;
    }
}

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

最佳答案

您混淆了 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"
  }
}

关于c# - 使用 Microsoft Graph 重置用户密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49851122/

相关文章:

angular 5 adal.js 自动 token 更新负载 Angular 两次或更多

azure - 缺少 Microsoft.Azure.Websites 服务主体?

azure - MS个人账户和企业账户的区别

c# - 如何将 DataTable.Select() 结果传递给新的 DataTable?

c# - 如何提高非虚拟化 DataGrid 的排序性能?

c# - 在 ASP.NET MVC View 中使用 node.js

c# - 排列不同列中的文本部分

javascript - AngularJS + ADAL.JS 设置资源 ID(受众)

azure - Microsoft Graph 处理按 ID 获取订阅的白色存储扩展时出错

c# - 如何使用 Microsoft Graph api 从 azure Active Directory 获取所有用户属性