在 ADB2C 中忘记密码 - (AADB2C90118),如何运行特定的用户流?

Das*_*ash 8 c# azure openid-connect azure-ad-b2c asp.net-core-2.2

问题

我正在尝试在我的应用程序中处理“重置密码”用户流程。单击“忘记密码”链接后,成功触发了 OnRemoteFailure OpenId 事件,成功重定向到指定的 url 'Home/ResetPassword',但不是重定向到 ADB2C 重置密码屏幕,而是重定向回登录/登录上页。

背景

注册/登录策略成功运行,但根据 Microsoft 文档:https://docs.microsoft.com/en-us/azure/active-directory-b2c/active-directory-b2c-reference-policies

"使用本地帐户注册或登录的用户流程在体验的第一页包含忘记密码?链接。单击此链接不会自动触发密码重置用户流程。

相反,错误代码 AADB2C90118 会返回到您的应用程序。您的应用程序需要通过运行重置密码的特定用户流来处理此错误代码。要查看示例,请查看一个简单的 ASP.NET 示例,该示例演示了用户流的链接。

Active Directory B2C 设置

应用设置

用户流

用户流

代码

OpenId事件

protected virtual Task OnRemoteFailure(RemoteFailureContext context)
{
    context.HandleResponse();
    // Handle the error code that Azure AD B2C throws when trying to reset a password from the login page
    // because password reset is not supported by a "sign-up or sign-in policy"
    if (context.Failure is OpenIdConnectProtocolException && context.Failure.Message.Contains("AADB2C90118"))
    {
        // If the user clicked the reset password link, redirect to the reset password route
        context.Response.Redirect("/Home/ResetPassword");                
    }
    else if (context.Failure is OpenIdConnectProtocolException && context.Failure.Message.Contains("access_denied"))
    {
        context.Response.Redirect("/");
    }
    else
    {
        context.Response.Redirect("/Home/Error?message=" + WebUtility.UrlEncode(context.Failure.Message));                
    }          

    return Task.FromResult(0);
}
Run Code Online (Sandbox Code Playgroud)

家庭控制器

public IActionResult ResetPassword()
{
    var redirectUrl = Url.Action(nameof(HomeController.Index), "Home");
    var properties = new AuthenticationProperties { RedirectUri = redirectUrl };
    properties.Items[AzureADB2COptionsExtended.PolicyAuthenticationProperty] = _adb2cOptions.ResetPasswordPolicyId;
    return Challenge(properties, AzureADB2CDefaults.AuthenticationScheme);
}
Run Code Online (Sandbox Code Playgroud)

OpenIdSignInSignUp

我发现很多使用 OWIN 的例子......关于 ASP.Net Core 2.2 w/ADB2C 的文档非常有限。

小智 1

注册登录策略现在内置了对密码重置的支持,无需第二个“密码重置”用户流程。那里的所有文档和示例非常令人困惑,但这是最新的文档,它对我们有用!

https://learn.microsoft.com/en-us/azure/active-directory-b2c/force-password-reset?pivots=b2c-user-flow