有什么办法可以使用多个策略对B2C应用程序进行身份验证

时间:2019-04-02 12:07:56

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

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

我当前的appsettings.json如下所示

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

},

1 个答案:

答案 0 :(得分:0)

您可以通过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

希望有帮助。