首页 > 解决方案 > 身份服务器 4 传递 acr_values 但不重定向到外部提供者

问题描述

我们有 Idsrv 4 和另一个外部身份验证提供程序。这两个系统之间的集成很好,我们可以登录/重定向一切正常。

但它涉及用户操作,他们仍然需要单击按钮来定义他们想要使用哪个外部提供程序。我们想为特定的用户组跳过此步骤,并将用户重定向到自动直接登录到外部提供程序。

我了解到这种自动重定向可以acr_values通过与客户端的授权请求一起使用来实现。我尝试使用它,但仍然没有重定向到外部提供商。

身份服务器设置:我们将AuthenticationScheme-> deoidsrv设置为我们的外部提供者

services.AddAuthentication()
                        .AddOpenIdConnect("demoidsrv", "IdentityServer", options =>
                        {
                            options.SignInScheme = IdentityServerConstants.ExternalCookieAuthenticationScheme;
                            options.SignOutScheme = IdentityServerConstants.SignoutScheme;

                            options.Authority = "https://demo.identityserver.io/";
                            options.ClientId = "login";
                            options.ResponseType = "id_token";
                            options.SaveTokens = true;

                            options.Scope.Add(IdentityServerConstants.StandardScopes.OpenId);
                            options.Scope.Add(IdentityServerConstants.StandardScopes.Profile);
                            options.Scope.Add(IdentityServerConstants.StandardScopes.Email);

                            options.CallbackPath = "/signin-idsrv";
                            options.SignedOutCallbackPath = "/signout-callback-idsrv";
                            options.RemoteSignOutPath = "/signout-idsrv";
                        })

我们在事件中传递了acr_values-> idp:demoidsrvOnRedirectToIdentityProvider。客户端 Mvc 应用 Startup.cs:

services.AddAuthentication(options =>
                {
                    options.DefaultScheme = "Cookies";
                    options.DefaultChallengeScheme = "oidc";
                })
                .AddCookie("Cookies", options =>
                {
                    options.AccessDeniedPath = "/AccessDenied";
                })
                .AddOpenIdConnect("oidc", options =>
                {
                    options.SignInScheme = "Cookies";
                    options.ClientId = "test.web.app1.hybrid";
                    options.ResponseType = "code id_token";
                    options.RequireHttpsMetadata = false; // to host it without Https
                    options.SaveTokens = true;
                    options.ClientSecret = "secret";

                    options.GetClaimsFromUserInfoEndpoint = true;
                    options.Scope.Add("openid");

                    //T1 Identity Server
                    options.Authority = Configuration.GetSection("MySettings").GetSection("IdentityServerUrl").Value;


                    options.Events = new OpenIdConnectEvents()
                    {
                        OnRedirectToIdentityProvider = ctx =>
                        {
                            ctx.ProtocolMessage.UiLocales = Thread.CurrentThread.CurrentUICulture.Name;
                            ctx.ProtocolMessage.AcrValues = "idp:demoidsrv";
                            return Task.CompletedTask;
                        }
                    };
                });

我检查了redirect_uri,它确实在AuthoriseRequest中正确附加了acr_values

http://localhost:5847/identityserver/connect/authorize?client_id=test.web.app1.hybrid&redirect_uri=http%3A%2F%2Flocalhost%3A64177%2Fsignin-oidc&response_type=code%20id_token&scope=openid%20offline_access&response_mode=form_post&nonce=637315373849113603&ui_locales= en-US&state=testStatewH& acr_values=idp%3Ademoidsrv &x-client-SKU=ID_NETSTANDARD2_0&x-client-ver=5.3.0.0

我想知道我是否需要通过检查此值来手动实现重定向,或者我是否缺少一些设置以使重定向自动发生?

标签: .net-coreidentityserver4

解决方案


据我所知,它不会自动重定向。我们必须检测发布的值并手动重定向到外部提供商。

如果您提供acr_values,它们可以在Idp属性中检索到AuthorizationContext。然后重定向到ChallengeAction的ExternalController模拟重定向。

var context = await _interaction.GetAuthorizationContextAsync(returnUrl);

// redirect to external Identity provider automatically, if requested
if (string.IsNullOrWhiteSpace(context.IdP) == false)
{
    var idp = context.IdP;
    return RedirectToAction("Challenge", "ExternalAuthentication",
        new
        {
            provider = idp,
            returnUrl
        });
}   

推荐阅读