【问题标题】:Forgot Password in ADB2C - (AADB2C90118), How to run a specific user flow?Forgot Password in ADB2C - (AADB2C90118),如何运行特定的用户流程?
【发布时间】:2019-12-15 23:46:42
【问题描述】:

问题

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

背景

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

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

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

Active Directory B2C 设置

用户流

代码

OpenIdEvent

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);
}

家庭控制器

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);
}

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

【问题讨论】:

  • 那么AzureADB2COptionsExtended.PolicyAuthenticationProperty这个值是多少?

标签: c# azure azure-ad-b2c openid-connect asp.net-core-2.2


【解决方案1】:

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

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

【讨论】:

  • 对...但是此流程确实说明了单击“忘记密码?”时会发生什么...
【解决方案2】:

问题中发布的答案:

对于遇到相同或类似问题的其他人,请务必留意 OpenIdConnectEvents。我们一直在试验 ADB2C/OpenID 并有测试代码。这段代码显然是无效的。

protected virtual Task OnRedirectToIdentityProvider(RedirectContext context)
{
    string policy = "";
    context.Properties.Items.TryGetValue(AzureADB2COptionsExtended.PolicyAuthenticationProperty, out policy);
    if (!string.IsNullOrEmpty(policy) && !policy.ToLower().Equals(_adb2cOptions.DefaultPolicy.ToLower()))
    {
        context.ProtocolMessage.Scope = OpenIdConnectScope.OpenId;
        context.ProtocolMessage.ResponseType = OpenIdConnectResponseType.IdToken;
        context.ProtocolMessage.IssuerAddress = context.ProtocolMessage.IssuerAddress.ToLower().Replace(_adb2cOptions.DefaultPolicy.ToLower(), policy.ToLower());
    }
    return Task.FromResult(0);
}

【讨论】:

    【解决方案3】:

    通常,您在 appsettings.json 中配置的 ResetPassword 流程会在使用 Microsoft.Identity.Web 包时自动调用。在你的情况下B2C_1_SSPR。这意味着您必须使用此 ID 定义自定义用户流。 (我猜 SSPR = 自助密码重置)

    在这种默认情况下,您唯一需要的是在您的 Startup 中调用以下代码:

    services.AddMicrosoftIdentityWebAppAuthentication(Configuration, "ActiveDirectoryB2C");
    

    这就像一个魅力。

    但是,您决定自己处理所有这些东西,而不是使用 Microsft.Identity.Web 库进行所有处理。 在这种情况下,您可以自行处理密码重置。

    但是让我们看看Microsft.Identity.Web。他们集成了一个 AccountController 来处理 B2C 自定义流的处理(在本例中为 ResetPassword 操作)。

    AADB2C90118 的异常处理可以在 Identity 包的the source code 中找到:

     if (isOidcProtocolException && message.Contains(ErrorCodes.B2CForgottenPassword))
     {
         // If the user clicked the reset password link, redirect to the reset password route
         context.Response.Redirect($"{context.Request.PathBase}/MicrosoftIdentity/Account/ResetPassword/{SchemeName}");
     }
    

    【讨论】:

      猜你喜欢
      • 2017-12-24
      • 1970-01-01
      • 2021-10-21
      • 2021-02-03
      • 2021-11-11
      • 1970-01-01
      • 2019-06-15
      • 2012-06-09
      • 1970-01-01
      相关资源
      最近更新 更多