Identity Server 4 传递 acr_values 但不重定向到外部提供者
Identity Server 4 Passing acr_values but not redirecting to external provider
我们有 Idsrv 4 和另一个外部身份验证提供程序。这两个系统之间的集成很好,我们可以正常登录/重定向。
但这涉及到用户操作,他们仍然需要点击按钮来定义他们想要使用哪个外部供应商。我们想为特定的用户组跳过此步骤,并重定向用户以自动直接登录到外部提供商。
我了解到这种自动重定向可以通过使用 acr_values
与来自客户端的授权请求一起传递来实现。我尝试使用它,但仍然没有重定向到外部提供商。
身份服务器设置:
我们设置 AuthenticationScheme
-> demoidsrv 作为我们的外部供应商
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";
})
我们在 OnRedirectToIdentityProvider
事件中通过了 acr_values
-> idp:demoidsrv。
客户端 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
我想知道我是否需要通过检查此值手动实现重定向,或者我是否缺少一些设置来自动进行重定向?
据我所知,它不会自动重定向。我们必须检测发布的值并手动重定向到外部提供商。
如果您提供 acr_values
,它们可以在 Idp
属性 的 AuthorizationContext
中检索。然后重定向到 ExternalController
的 Challenge
动作来模拟重定向。
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
});
}
我们有 Idsrv 4 和另一个外部身份验证提供程序。这两个系统之间的集成很好,我们可以正常登录/重定向。
但这涉及到用户操作,他们仍然需要点击按钮来定义他们想要使用哪个外部供应商。我们想为特定的用户组跳过此步骤,并重定向用户以自动直接登录到外部提供商。
我了解到这种自动重定向可以通过使用 acr_values
与来自客户端的授权请求一起传递来实现。我尝试使用它,但仍然没有重定向到外部提供商。
身份服务器设置:
我们设置 AuthenticationScheme
-> demoidsrv 作为我们的外部供应商
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";
})
我们在 OnRedirectToIdentityProvider
事件中通过了 acr_values
-> idp:demoidsrv。
客户端 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_valueshttp://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
我想知道我是否需要通过检查此值手动实现重定向,或者我是否缺少一些设置来自动进行重定向?
据我所知,它不会自动重定向。我们必须检测发布的值并手动重定向到外部提供商。
如果您提供 acr_values
,它们可以在 Idp
属性 的 AuthorizationContext
中检索。然后重定向到 ExternalController
的 Challenge
动作来模拟重定向。
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
});
}