【问题标题】:Is there any way to Authenticate B2C application using multiple policies有没有办法使用多个策略来验证 B2C 应用程序
【发布时间】:2019-08-23 18:13:26
【问题描述】:

我正在使用 B2C 登录页面来验证我的用户。这些用户根据他们的业务选择了多个 IDP,我使用选定的 IDP 创建了多个策略。在基于用户电子邮件的登录页面中,我显示了他的登录页面,其中只有他的相关 IDP。但在我的 Web 应用程序中,我只能在 appsettings.json 中添加 一个 Signup or SignIn policy 来对用户进行身份验证。是否有任何选项可以在 appsettings.json 文件中包含多个策略或任何其他方式来处理此要求

我当前的 appsettings.json 如下所示

"AzureAdB2C": {
"Instance": "https://login.microsoftonline.com/tfp/",
"ClientId": "******-***-****-****-*******",
"Domain": "mycustomdomain.onmicrosoft.com",
"SignUpSignInPolicyId": "Org-signinsignout"

},

【问题讨论】:

标签: azure asp.net-core azure-ad-b2c


【解决方案1】:

您可以通过passing the requested policy from a controller method to the authentication middleware 为不同类型的用户调用不同的策略:

  public IActionResult LogInForBusinessCustomer(string uiLocale)
        {
            return LogInFor(Constants.AuthenticationSchemes.B2COpenIdConnect, Constants.Policies.SignUpOrSignInWithWorkAccount, uiLocale);
        }

    public IActionResult LogInForIndividualCustomer(string uiLocale)
    {
        return LogInFor(Constants.AuthenticationSchemes.B2COpenIdConnect, Constants.Policies.SignUpOrSignInWithPersonalAccount, uiLocale);
    }

    public IActionResult LogInForPartner(string uiLocale)
    {
        return LogInFor(Constants.AuthenticationSchemes.B2BOpenIdConnect, null, uiLocale);
    }

private IActionResult LogInFor(string authenticationScheme, string policy)
{
    if (!User.Identity.IsAuthenticated)
    {
        return new ChallengeResult(
            authenticationScheme,
            new AuthenticationProperties(
                new Dictionary<string, string>
                {
                    {Constants.AuthenticationProperties.Policy, policy}
                })
            {
                RedirectUri = Url.Action("LoggedIn", "Account", values: null, protocol: Request.Scheme)
            });
    }

    return RedirectToHome();
}

然后在身份验证中间件中为请求的策略设置重定向 URL:

OnRedirectToIdentityProvider = async context =>
            {
                var policy = context.Properties.Items.ContainsKey(Constants.AuthenticationProperties.Policy) ? context.Properties.Items[Constants.AuthenticationProperties.Policy] : Constants.Policies.SignUpOrSignInWithPersonalAccount;
                var configuration = await GetB2COpenIdConnectConfigurationAsync(context, policy);
                context.ProtocolMessage.IssuerAddress = configuration.AuthorizationEndpoint;

                if (context.Properties.Items.ContainsKey(Constants.AuthenticationProperties.UILocales))
                {
                    context.ProtocolMessage.SetParameter("ui_locales", context.Properties.Items[Constants.AuthenticationProperties.UILocales]);
                }

                context.ProtocolMessage.SetParameter("dc", "cdm");
                context.ProtocolMessage.SetParameter("slice", "001-000");
            },

参考:https://github.com/Azure-Samples/active-directory-external-identities-woodgrove-demo/blob/2b5110c25d1a626bf9b9ac27ecaaabad8b4bccf4/src/WoodGroveGroceriesWebApplication/Startup.cs#L283

也请看看这个帖子

Azure B2C - Single App with multiple login for different user types setup in Azure

希望对你有帮助。

【讨论】:

  • 当请求到达服务器时,首先我需要对 B2C 用户进行身份验证,要进行身份验证,我需要知道 SignuP 或 SignIn 策略名称。在那之后,只有我可以阅读我不知道政策是什么的声明。
  • 您是否在我上面发布的示例中检查了此站点woodgrovegroceriesb2c.azurewebsites.net,基本上您需要保留不同的登录选项,当用户单击选项时,您需要调用受尊重的策略。
猜你喜欢
  • 1970-01-01
  • 2015-06-19
  • 1970-01-01
  • 2023-01-31
  • 2019-10-10
  • 2021-08-15
  • 2021-03-23
  • 2020-01-02
  • 1970-01-01
相关资源
最近更新 更多