[英]Asp.NET 4.7.2 Multiple Owin Auth Providers
是否可以在同一个应用程序中使用两个 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 provider
HttpContext.GetOwinContext().Authentication.Challenge(
new AuthenticationProperties { RedirectUri = "/" },
OpenIdConnectAuthenticationDefaults.AuthenticationType);
致电客户提供商
var properties = new AuthenticationProperties { RedirectUri = "/" }; var scheme = "schemeName"; HttpContext.GetOwinContext().Authentication.Challenge(properties, scheme);
如何调用适当的身份验证提供程序?
谢谢
您需要通过OpenIdConnectAuthenticationOptions.AuthenticationType
属性为每个身份验证中间件设置不同的方案,并在Challenge(...)
方法中传递要进行身份验证的方案。
我在更新 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
}
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.