Asp.NET 4.7.2多个Owin身份验证提供程序

时间:2020-04-16 23:46:36

标签: c# asp.net owin

是否可以在同一应用程序中使用两个OpenIdConnect提供程序?我需要针对两个不同的组进行登录,第一个是具有有效Azure AD帐户的员工,第二个是没有Azure AD帐户的客户。我知道要使用的端点,并且已经使用.NET Core处理了包含此功能的应用程序,但是我无法在.NET 4.7.2中成功实现此功能。

在我的start.auth.cs文件中,我试图添加像这样的提供程序

app.UseOpenIdConnectAuthentication(CustomerOptions());
app.UseOpenIdConnectAuthentication(EmployeeOptions());

    private static OpenIdConnectAuthenticationOptions EmployeeOptions() =>
            new OpenIdConnectAuthenticationOptions
            {
                ClientId = ClientId,
                Authority = authority,
                RedirectUri = RedirectUri,
                ClientSecret = ClientSecret,
                PostLogoutRedirectUri = RedirectUri,
                Scope = OpenIdConnectScope.OpenIdProfile,
                // ResponseType is set to request the id_token - which contains basic information about the signed-in user
                ResponseType = OpenIdConnectResponseType.CodeIdToken,
                // ValidateIssuer set to false to allow personal and work accounts from any organization to sign in to your application
                // To only allow users from a single organizations, set ValidateIssuer to true and 'tenant' setting in web.config to the tenant name
                // To allow users from only a list of specific organizations, set ValidateIssuer to true and use ValidIssuers parameter
                TokenValidationParameters = new TokenValidationParameters()
                {
                    ValidateIssuer = false // This is a simplification
                },
                // OpenIdConnectAuthenticationNotifications configures OWIN to send notification of failed authentications to OnAuthenticationFailed method
                Notifications = new OpenIdConnectAuthenticationNotifications
                {
                    AuthenticationFailed = OnAuthenticationFailed,
                    SecurityTokenValidated = OnAdSecurityTokenValidated
                }
            };

其中... Options方法具有特定于每个端点的OpenIdConnectAuthenticationOptions。如果我仅使用一种方法就可以向应用程序进行身份验证,但是当我尝试同时添加这两种身份验证时,只会使用最后添加的客户端。

调用方法的代码是: 1.致电Azure AD提供程序

            HttpContext.GetOwinContext().Authentication.Challenge(
                new AuthenticationProperties { RedirectUri = "/" },
                OpenIdConnectAuthenticationDefaults.AuthenticationType);
  1. 致电客户提供商

            var properties = new AuthenticationProperties { RedirectUri = "/" };
            var scheme = "schemeName";
            HttpContext.GetOwinContext().Authentication.Challenge(properties, scheme);
    

如何获得相应的身份验证提供程序?

谢谢

2 个答案:

答案 0 :(得分:1)

您需要通过OpenIdConnectAuthenticationOptions.AuthenticationType属性为每个身份验证中间件设置不同的方案,并以Challenge(...)方法传递要验证的方案。

答案 1 :(得分:0)

在更新OpenIdConnectAuthenticationOptions时,我忽略了设置身份验证类型参数,因此在添加第二个身份验证提供程序时,我覆盖了默认设置。

app.UseOpenIdConnectAuthentication(CustomerOptions());
app.UseOpenIdConnectAuthentication(EmployeeOptions());

private static OpenIdConnectAuthenticationOptions EmployeeOptions() =>
        new OpenIdConnectAuthenticationOptions("employeeAuthenticationType")
        {
            ClientId = ClientId,
            Authority = authority,
            RedirectUri = RedirectUri,
            ClientSecret = ClientSecret,
            PostLogoutRedirectUri = RedirectUri,
            Scope = OpenIdConnectScope.OpenIdProfile,
            // ResponseType is set to request the id_token - which contains basic information about the signed-in user
            ResponseType = OpenIdConnectResponseType.CodeIdToken,
            // ValidateIssuer set to false to allow personal and work accounts from any organization to sign in to your application
            // To only allow users from a single organizations, set ValidateIssuer to true and 'tenant' setting in web.config to the tenant name
            // To allow users from only a list of specific organizations, set ValidateIssuer to true and use ValidIssuers parameter
            TokenValidationParameters = new TokenValidationParameters()
            {
                ValidateIssuer = false // This is a simplification
            },
            // OpenIdConnectAuthenticationNotifications configures OWIN to send notification of failed authentications to OnAuthenticationFailed method
            Notifications = new OpenIdConnectAuthenticationNotifications
            {
                AuthenticationFailed = OnAuthenticationFailed,
                SecurityTokenValidated = OnAdSecurityTokenValidated
            }
        };