当您使用外部提供者配置 identtyserver 时,通常在 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"],
});
然后在客户端应用程序中,您可以将acrvaluesAuthentication-type 设置为如下所示
app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
{
Notifications = new OpenIdConnectAuthenticationNotifications
{
RedirectToIdentityProvider = (n) =>
{
if (n.ProtocolMessage.RequestType == OpenIdConnectRequestType.AuthenticationRequest)
{
if(n.Request.Uri == "someurl")
{
//set acrvalues. the value of the `idp`, (which is `Google` in this case) must match with the `AutheticationType` you set in IdentityServer
n.ProtocolMessage.AcrValues = "idp:Google";
}
}
return Task.FromResult(0);
}
}
另请注意,该idp值区分大小写。
另一个选项(我没有尝试过)。而不是设置idp您tenant在客户端应用程序中设置。
n.ProtocolMessage.AcrValues = "tenant:" + n.Request.Uri.ToString();
正如@TheRock 提到的,在 IndentityServer 中检查租户SignInMessage并覆盖Idp
public override Task PreAuthenticateAsync(PreAuthenticationContext context)
{
if(context.SignInMessage.Tenant = "sometenant")
{
context.SignInMessage.IdP = "Google";
return base.PreAuthenticateAsync(context);
}
}
这样,当您不断添加新的外部提供程序时,您不必更改客户端应用程序中的代码。您只需更新 IndentityServer 代码。如果您有多个客户端应用程序连接到同一个身份服务器,这尤其有用。