如何在Azure活动目录中使用多个OpenIdConnectAuthenticationOptions

时间:2017-05-08 23:09:37

标签: c# azure oauth-2.0 azure-active-directory openid-connect

在我们的项目中,我们希望显示两个选项(登录为员工,登录为客户)。根据选择,我们希望使用Azure Active Directory B2B或Azure B2C对用户进行身份验证。

点击链接后,我可以将身份验证模式设置为被动并打开登录页面。配置单个OpenIdConnectAuthenticationOptions时,它可以正常工作。但是,当我配置多个OpenIdConnectAuthenticationOptions时,这不起作用。

        app.UseOpenIdConnectAuthentication(
            new OpenIdConnectAuthenticationOptions
            {
                AuthenticationMode = AuthenticationMode.Passive,
                MetadataAddress = String.Format(aadInstance2, tenant2, SignUpSignInPolicyId),
                ClientId = clientId2,
                RedirectUri = redirectUri2,
                PostLogoutRedirectUri = postLogoutRedirectUri,
            });

        app.UseOpenIdConnectAuthentication(
            new OpenIdConnectAuthenticationOptions
            {
                AuthenticationMode = AuthenticationMode.Passive,
                ClientId = clientId,
                Authority = authority,
                PostLogoutRedirectUri = postLogoutRedirectUri,
            });

    public void Redirect()
    {
       HttpContext.GetOwinContext().Authentication.Challenge(new AuthenticationProperties { RedirectUri = "https://localhost/WebApp1/" }, OpenIdConnectAuthenticationDefaults.AuthenticationType);
    }

1 个答案:

答案 0 :(得分:1)

您可以尝试使用AuthenticationType。此属性标识管道中的此中间件,并用于将其引用以进行身份​​验证操作。例如,您可以定义如下配置:

app.UseOpenIdConnectAuthentication(
    new OpenIdConnectAuthenticationOptions("AADLogin")
    {
        AuthenticationMode = AuthenticationMode.Passive,
        MetadataAddress = String.Format(aadInstance2, tenant2, SignUpSignInPolicyId),
        ClientId = clientId2,
        RedirectUri = redirectUri2,
        PostLogoutRedirectUri = postLogoutRedirectUri,
    });

app.UseOpenIdConnectAuthentication(
    new OpenIdConnectAuthenticationOptions("B2CLogin")
    {
        AuthenticationMode = AuthenticationMode.Passive,
        ClientId = clientId,
        Authority = authority,
        PostLogoutRedirectUri = postLogoutRedirectUri,
    });

然后取决于用户选择,您可以选择使用哪一个:

    if ()
    {
        HttpContext.GetOwinContext()
            .Authentication.Challenge(new AuthenticationProperties {RedirectUri = "/"},
               "AADLogin");
    }
    else
    {
        HttpContext.GetOwinContext()
           .Authentication.Challenge(new AuthenticationProperties { RedirectUri = "/" },
              "B2CLogin");
    }