【问题标题】:Reset Azure AD password through Web API instead of Graph API通过 Web API 而不是 Graph API 重置 Azure AD 密码
【发布时间】:2021-07-19 21:59:16
【问题描述】:

无法使用 Graph API 更改密码

Unsupported end-user operations
    Any end user resetting their own password by using PowerShell version 1, version 2, or the Microsoft Graph API.

https://docs.microsoft.com/en-us/azure/active-directory/authentication/concept-sspr-writeback#unsupported-writeback-operations

在阅读上述内容之前,我尝试使用以下代码更改密码:

实例化 GraphServiceClient

string[] scopes = new string[] { "User.Read", "Directory.AccessAsUser.All", "User.Invite.All" };

IPublicClientApplication app = PublicClientApplicationBuilder.Create(clientId)
    .WithAuthority(authority)
    .WithTenantId(tenantId)
    .Build();

var securePassword = new SecureString();
foreach (char c in user.Password.ToCharArray())  // you should fetch the password
    securePassword.AppendChar(c);  // keystroke by keystroke

var tokens = app.AcquireTokenByUsernamePassword(scopes, user.UserName, securePassword).ExecuteAsync().Result;

graphClient = new GraphServiceClient(
    new DelegateAuthenticationProvider(x =>
    {
        x.Headers.Authorization = new AuthenticationHeaderValue(
            "Bearer", tokens.AccessToken);

        return Task.FromResult(0);
    }));

更改密码尝试:

public async Task<ActionResult> ResetPassword([FromBody] JObject data)
{
    try
    {
        await graphClient.Me
        .ChangePassword(data["currentPassword"].ToString(), data["newPassword"].ToString())
        .Request()
        .PostAsync();
...
}

允许用户重置密码的步骤(以编程方式/Azure 门户)是什么?是否有其他方法可以在不使用 Graph API 的情况下更改密码?

【问题讨论】:

  • 我可以使用graph api更改密码。
  • 您使用graph api修改密码时有没有报错?

标签: azure asp.net-core asp.net-web-api azure-active-directory microsoft-graph-api


【解决方案1】:

graph api绝对可以重置密码:

POST https://graph.microsoft.com/v1.0/me/changePassword
Content-Type: application/json
{
  "currentPassword": "Test1234!",
  "newPassword": "Test5678!"
}

GraphServiceClient graphClient = new GraphServiceClient( authProvider );

var currentPassword = "{currentPassword}";

var newPassword = "{newPassword}";

await graphClient.Me
    .ChangePassword(currentPassword,newPassword)
    .Request()
    .PostAsync();

【讨论】:

  • 本次请求的状态码为200,因此成功通过。问题是我仍然可以使用旧密码登录。使用新密码返回“发生一个或多个错误。(自动登录时的联合服务.../winauth/trust/2005/...返回错误:身份验证失败)”
  • @Tassisto 很奇怪,我刚测试了一下,改密码成功了。可以用graph-explorer测试吗?
  • 仍然没有更新,请求通过并返回 200 状态码。登录时我仍然使用旧密码。
  • 嗨@Carl Zhao 现在它显示“代码:Request_ResourceNotFound\r\n消息:资源 'guid' 不存在或其查询的引用属性对象之一不存在。\r\n内部错误: \r\n\tAdditionalData:\r\n\tdate: 2021-07-07T11:46:11\r\n\trequest-id: guid\r\n\tclient-request-id: guid\r\nClientRequestId:指导\r\n"
  • 您是否在 Azure AD Connect 中启用了 SSPR 和重置密码?您是否允许将密码写回本地?
【解决方案2】:

您可以使用 Graph API 重置密码。以下是要遵循的步骤。 1. 使用适当的权限(例如,读/写用户详细信息)向 AD 注册您的 Web 应用程序。使用 MFA 2. 使用图形 API 进行重置。https://docs.microsoft.com/en-us/previous-versions/azure/ad/graph/api/users-operations#ResetUserPassword.AD 也可以使用 passwordProfile 资源类型而是。

【讨论】:

  • 图形 API - 你的意思是图形 API ?
【解决方案3】:

我的解决方案是为 Azure AD Connect 配置帐户权限

我必须在 Azure AD Connect 中启用密码写回;-)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-19
    • 2018-01-16
    • 2016-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多