当您使用外部提供程序配置IdentityServer时,在AuthenticationOptions中,您通常将AutheticationType
设置为某个字符串。就像下面这样
app.UseGoogleAuthentication(new GoogleOAuth2AuthenticationOptions
{
AuthenticationType = "Google",
Caption = "Sign-in with Google",
SignInAsAuthenticationType = signInAsType,
ClientId = ConfigurationManager.AppSettings["google:clientid"],
ClientSecret = ConfigurationManager.AppSettings["google:clientsecret"],
});
然后在客户端应用程序中,您可以将acrvalues
设置为以下类似的身份验证类型
app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
{
Notifications = new OpenIdConnectAuthenticationNotifications
{
RedirectToIdentityProvider = (n) =>
{
if (n.ProtocolMessage.RequestType == OpenIdConnectRequestType.AuthenticationRequest)
{
if(n.Request.Uri == "someurl")
{
n.ProtocolMessage.AcrValues = "idp:Google";
}
}
return Task.FromResult(0);
}
}
请注意,
idp
的值区分大小写。
另一种选择(我没有尝试过)。您可以在客户端应用程序中设置
租户
而不是设置
idp
。
n.ProtocolMessage.AcrValues = "tenant:" + n.Request.Uri.ToString()
正如@TheRock所提到的,在IdentityServer中检查SignInMessage
中的租户并覆盖Idp
public override Task PreAuthenticateAsync(PreAuthenticationContext context)
{
if(context.SignInMessage.Tenant = "sometenant")
{
context.SignInMessage.IdP = "Google";
return base.PreAuthenticateAsync(context);
}
}
通过这种方式,当您不断添加新的外部提供者时,您无需更改客户端应用程序中的代码。您只需要更新IdentityServer代码即可。如果您有多个客户端应用程序连接到同一个身份验证服务器,这将特别有帮助。