【问题标题】:How to use multiple OpenIdConnectAuthenticationOptions in Azure active directory如何在 Azure 活动目录中使用多个 OpenIdConnectAuthenticationOptions
【发布时间】:2017-10-07 02:26:36
【问题描述】:

在我们的项目中,我们希望显示两个选项(以员工身份登录和以客户身份登录)。根据选择,我们希望使用 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);
    }

【问题讨论】:

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


    【解决方案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");
        }
    

    【讨论】:

    • 能否请您告诉我,在这种情况下如何获取用户身份?
    • 您的问题似乎已在this thread 中解决。
    猜你喜欢
    • 2015-01-17
    • 1970-01-01
    • 2023-02-22
    • 1970-01-01
    • 2019-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多