16

我们已将客户端应用程序配置为通过 OpenID Connect 协议使用 IdentityServer3 身份验证(它是使用 OWIN 中间件支持 OIDC 的 ASP.NET MVC 应用程序)。

IdentityServer3 本身被配置为使用本地登录和外部登录(例如 Azure AD)。

在常规流程中,一旦应用程序需要对用户进行身份验证,它就会将他重定向到 IdentityServer3 登录屏幕——这很好。但在某些情况下,在每个请求的基础上,我想通过某种方式让 IdentityServer3 知道用户想要立即使用特定的外部身份提供者登录来绕过登录屏幕。

那有可能吗?

图片

4

3 回答 3

19

刚刚在IdentityServer3 的授权/身份验证端点文档中找到了解决方案!

acr_values(可选)允许将额外的身份验证相关信息传递给用户服务 - 还有一些具有特殊含义的值:idp:name_of_idp 绕过登录/主域屏幕 并将用户直接转发到选定的身份提供者(如果每个客户端配置允许) ) tenant:name_of_tenant 可用于将租户名称传递给用户服务

如何使用 OWIN OpenID Connect 中间件传递附加参数:https ://katanaproject.codeplex.com/workitem/325

这是授权请求的示例:

样品要求

于 2015-05-02T06:55:18.290 回答
7

我知道这很旧,但我认为如果他们想自动重定向到外部登录,我仍然会把它放在这里帮助某人:

public override Task PreAuthenticateAsync(PreAuthenticationContext context)
{
    context.SignInMessage.IdP = "windows";
    return base.PreAuthenticateAsync(context);  
}

您基本上可以覆盖 UserServiceBase 上的 PreAuthenticateAsync 并将 context.SignInMessage 上的属性 IdP 更改为在您的启动中设置的外部提供程序名称。这将重定向。

于 2016-12-02T18:26:09.280 回答
0

当您使用外部提供者配置 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值区分大小写。

另一个选项(我没有尝试过)。而不是设置idptenant在客户端应用程序中设置。

   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 代码。如果您有多个客户端应用程序连接到同一个身份服务器,这尤其有用。

于 2018-09-05T18:20:11.227 回答