如何在Azure Active Directory中使用多个OpenIdConnectAuthenticationOptions

How to use multiple OpenIdConnectAuthenticationOptions in Azure active directory(如何在Azure Active Directory中使用多个OpenIdConnectAuthenticationOptions)
本文介绍了如何在Azure Active Directory中使用多个OpenIdConnectAuthenticationOptions的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

推荐答案

您可以尝试使用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");
    }

这篇关于如何在Azure Active Directory中使用多个OpenIdConnectAuthenticationOptions的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)