有没有办法从身份服务器中的客户端更新 IDP 会话令牌
Is there a way to renew IDP session token from a Client in Identity Server
我正在使用 Identity Server 为我公司的应用程序实现单点登录 In/Out。有什么方法可以使客户端的 cookie 更新(通过滑动过期)时,它 也会转到 IDP 并更新其会话 cookie 的过期时间 ?目标是即使在 IDP 会话应该过期之后,也能够在所有应用程序中共享 1 小时的滑动过期。
我现在能想到的唯一方法是创建一些中间件来检查客户端上的 cookie,如果它快要过期,则添加一个 iframe 来调用 IDP 上的端点以告诉它续订它也是饼干。我在正确的轨道上吗? Identity Server 中是否内置了这样的机制?如果有,端点是什么?
为清楚起见编辑:
问题
- 转到客户 A @ 10:00
- 重定向到 IDP 并登录
- 重定向回客户端
- 现在在客户端 A 和 IDP 上进行会话,直到 10:10
- 在客户端 A 上保持活跃直到 10:15 然后转到客户端 B
- 用户必须重新登录,因为客户端 A 站点已保持其 cookie 处于活动状态但 IDP 已过期 - 这是我希望客户端 A 调用的地方在 IDP 上滑动以保持它的会话与任何客户端一起处于活动状态,我可以转到客户端 B 而无需再次登录
是的,您可以请求刷新令牌,然后使用它来获取新的访问令牌。
我的情况和@Brock Allen
的答案解决的不一样。由于我们在内部使用身份验证,所以我需要在用户浏览网站时保持 STS 会话 cookie 处于活动状态。这样,如果 STS cookie 在一小时后过期,但用户在任何客户端站点上的活动时间超过 1 小时,他们就可以转到另一个客户端站点,而不必再次登录 STS。
我在IDP上用下面的代码解决了这个问题:
public class Startup
{
//...
public void Configuration(IAppBuilder app)
{
//...
app.Map("/identity", appBuilder =>
{
var idSrvOptions = new IdentityServerOptions
{ /* Your IdSrv config options here */ };
appBuilder.UseIdentityServer(idSrvOptions);
appBuilder.Map("/slide", identityAppBuilder =>
{
identityAppBuilder.Run(async context =>
{
var cookie = context.Environment.ResolveDependency<SessionCookie>();
cookie.ClearSessionId();
cookie.IssueSessionId(null);
await Task.FromResult(0);
});
});
});
}
}
来自 Client/Relying 方我有以下 JavaScript(它只是在 html 的主体中放置了一个不可见的图像,如果用户处于活动状态 - 这将滑动 STS 的 cookie w/o 而不必担心 iframe)。这可能会被放在中间件中以自动将其注入所有包含 <body>
标记的 html 响应或简单地添加到布局文件中:
(function () {
this.cookieDuration = 10000;
this.lastAction = new Date();
this.img = document.createElement('img');
this.img.src = 'https://myStsDomain.com/identity/slide';
this.img.setAttribute('style', 'display: none');
document.body.appendChild(img);
setInterval(refreshStsCookie, cookieDuration * .5);
document.addEventListener('click',
function() {
if (isLastActionLessThanCookieDuration()) {
lastAction = new Date();
}
});
function isLastActionLessThanCookieDuration() {
return (new Date() - lastAction) < cookieDuration;
}
function refreshStsCookie() {
if (isLastActionLessThanCookieDuration()) {
img.src = img.src;
}
}
})()
我正在使用 Identity Server 为我公司的应用程序实现单点登录 In/Out。有什么方法可以使客户端的 cookie 更新(通过滑动过期)时,它 也会转到 IDP 并更新其会话 cookie 的过期时间 ?目标是即使在 IDP 会话应该过期之后,也能够在所有应用程序中共享 1 小时的滑动过期。
我现在能想到的唯一方法是创建一些中间件来检查客户端上的 cookie,如果它快要过期,则添加一个 iframe 来调用 IDP 上的端点以告诉它续订它也是饼干。我在正确的轨道上吗? Identity Server 中是否内置了这样的机制?如果有,端点是什么?
为清楚起见编辑:
问题
- 转到客户 A @ 10:00
- 重定向到 IDP 并登录
- 重定向回客户端
- 现在在客户端 A 和 IDP 上进行会话,直到 10:10
- 在客户端 A 上保持活跃直到 10:15 然后转到客户端 B
- 用户必须重新登录,因为客户端 A 站点已保持其 cookie 处于活动状态但 IDP 已过期 - 这是我希望客户端 A 调用的地方在 IDP 上滑动以保持它的会话与任何客户端一起处于活动状态,我可以转到客户端 B 而无需再次登录
是的,您可以请求刷新令牌,然后使用它来获取新的访问令牌。
我的情况和@Brock Allen
的答案解决的不一样。由于我们在内部使用身份验证,所以我需要在用户浏览网站时保持 STS 会话 cookie 处于活动状态。这样,如果 STS cookie 在一小时后过期,但用户在任何客户端站点上的活动时间超过 1 小时,他们就可以转到另一个客户端站点,而不必再次登录 STS。
我在IDP上用下面的代码解决了这个问题:
public class Startup
{
//...
public void Configuration(IAppBuilder app)
{
//...
app.Map("/identity", appBuilder =>
{
var idSrvOptions = new IdentityServerOptions
{ /* Your IdSrv config options here */ };
appBuilder.UseIdentityServer(idSrvOptions);
appBuilder.Map("/slide", identityAppBuilder =>
{
identityAppBuilder.Run(async context =>
{
var cookie = context.Environment.ResolveDependency<SessionCookie>();
cookie.ClearSessionId();
cookie.IssueSessionId(null);
await Task.FromResult(0);
});
});
});
}
}
来自 Client/Relying 方我有以下 JavaScript(它只是在 html 的主体中放置了一个不可见的图像,如果用户处于活动状态 - 这将滑动 STS 的 cookie w/o 而不必担心 iframe)。这可能会被放在中间件中以自动将其注入所有包含 <body>
标记的 html 响应或简单地添加到布局文件中:
(function () {
this.cookieDuration = 10000;
this.lastAction = new Date();
this.img = document.createElement('img');
this.img.src = 'https://myStsDomain.com/identity/slide';
this.img.setAttribute('style', 'display: none');
document.body.appendChild(img);
setInterval(refreshStsCookie, cookieDuration * .5);
document.addEventListener('click',
function() {
if (isLastActionLessThanCookieDuration()) {
lastAction = new Date();
}
});
function isLastActionLessThanCookieDuration() {
return (new Date() - lastAction) < cookieDuration;
}
function refreshStsCookie() {
if (isLastActionLessThanCookieDuration()) {
img.src = img.src;
}
}
})()