IdentityServer4 中的动态 post_logout_uri 和 redirect_uri
Dynamic post_logout_uri and redirect_uri in IdentityServer4
我遇到一个问题,我的一位客户正在发送带有动态参数的 postlogouturi。
我已经从 IdentityServer4 端在 ClientStore 中注册了一个客户端
new Client({
clientId: "some_id",
redirectUri: {"https://www.example.com/callback1","https://www.example.com/callback2"},
postLogoutUri: {"https://www.example.com/postlogout1","https://www.example.com/postlogout2"},
... some other config,
})
这里的客户端是一个 React 应用程序,他们使用 oidc-client 库来连接身份服务器
如果他们使用以下配置,一切正常,我们可以在 IdentityServer 端获取 postlogouturi,并在他们注销后将客户端重定向到 URL。
var config = {
authority: "https://www.ouridentityserver.com",
client_id: "some_id",
redirect_uri: "https://www.example.com/callback1",
response_type: "id_token token",
scope:"openid profile api1",
post_logout_redirect_uri : "https://www.example.com/postlogout1",
};
var mgr = new Oidc.UserManager(config);
但是客户的要求是在上面的configpost-logout-Uri中添加一些查询参数
post_logout_redirect_uri : "https://www.example.com/postlogout1?language=chinese¶m1=value1¶m2=value2"
这里的 language、value1 和 value2 是动态的,因此我们无法在 IdentityServer 端使用那些确切的 post-logout-uris 注册客户端。
因此,每当他们在 postlogouturi 上使用这些动态参数时,IdentityServer 都会将 postlogouturi 获取为 null,并且在注销时无法重定向到该 URI。
有人可以帮助我了解是否有任何方法可以在 IdentityServer 端验证该动态 uris?提前致谢:)
在进行了一些研发之后,我了解了如何使用 HttpContextAccessor 找到解决方案。
每当任何用户从客户端单击注销时,请求就会到达 IdentityServer,我们可以使用以下代码动态获取客户端的 post_logout_redirect_uri。
var dynamicPostLogoutUri = _httpContextAccessor.HttpContext.Request.Query["post_logout_redirect_uri"].ToString();
一旦我们获得 post_logout_redirect_uri(具有动态查询参数),我们就可以在运行时为该客户端动态地在 ClientStore 上注册它。
new Client({
clientId: "some_id",
redirectUris: {"https://www.example.com/callback1","https://www.example.com/callback2"},
postLogoutUris: {dynamicPostLogoutUri ,"https://www.example.com/postlogout1","https://www.example.com/postlogout2"},
... some other config,
})
我正在分享这个解决方案,因为我认为这个答案可能对其他人有用。
编辑 1: 在 clientStore 中注册动态 postlogouturi 之前,您可以编写您的获胜逻辑来验证 uri。
if(ourValidation(postlogouturi)){
postLogoutUris.Add(dynamicPostLogoutUri);
}
我遇到一个问题,我的一位客户正在发送带有动态参数的 postlogouturi。
我已经从 IdentityServer4 端在 ClientStore 中注册了一个客户端
new Client({
clientId: "some_id",
redirectUri: {"https://www.example.com/callback1","https://www.example.com/callback2"},
postLogoutUri: {"https://www.example.com/postlogout1","https://www.example.com/postlogout2"},
... some other config,
})
这里的客户端是一个 React 应用程序,他们使用 oidc-client 库来连接身份服务器
如果他们使用以下配置,一切正常,我们可以在 IdentityServer 端获取 postlogouturi,并在他们注销后将客户端重定向到 URL。
var config = {
authority: "https://www.ouridentityserver.com",
client_id: "some_id",
redirect_uri: "https://www.example.com/callback1",
response_type: "id_token token",
scope:"openid profile api1",
post_logout_redirect_uri : "https://www.example.com/postlogout1",
};
var mgr = new Oidc.UserManager(config);
但是客户的要求是在上面的configpost-logout-Uri中添加一些查询参数
post_logout_redirect_uri : "https://www.example.com/postlogout1?language=chinese¶m1=value1¶m2=value2"
这里的 language、value1 和 value2 是动态的,因此我们无法在 IdentityServer 端使用那些确切的 post-logout-uris 注册客户端。 因此,每当他们在 postlogouturi 上使用这些动态参数时,IdentityServer 都会将 postlogouturi 获取为 null,并且在注销时无法重定向到该 URI。
有人可以帮助我了解是否有任何方法可以在 IdentityServer 端验证该动态 uris?提前致谢:)
在进行了一些研发之后,我了解了如何使用 HttpContextAccessor 找到解决方案。 每当任何用户从客户端单击注销时,请求就会到达 IdentityServer,我们可以使用以下代码动态获取客户端的 post_logout_redirect_uri。
var dynamicPostLogoutUri = _httpContextAccessor.HttpContext.Request.Query["post_logout_redirect_uri"].ToString();
一旦我们获得 post_logout_redirect_uri(具有动态查询参数),我们就可以在运行时为该客户端动态地在 ClientStore 上注册它。
new Client({
clientId: "some_id",
redirectUris: {"https://www.example.com/callback1","https://www.example.com/callback2"},
postLogoutUris: {dynamicPostLogoutUri ,"https://www.example.com/postlogout1","https://www.example.com/postlogout2"},
... some other config,
})
我正在分享这个解决方案,因为我认为这个答案可能对其他人有用。
编辑 1: 在 clientStore 中注册动态 postlogouturi 之前,您可以编写您的获胜逻辑来验证 uri。
if(ourValidation(postlogouturi)){
postLogoutUris.Add(dynamicPostLogoutUri);
}