简体   繁体   English

如何在ServiceStack中提前会话超时

[英]How to advance the session timeout in ServiceStack

The authentication, repository and caching providers in ServiceStack provide a simple way to add login sessions to a web application with almost no additional code. ServiceStack中的身份验证,存储库和缓存提供程序提供了一种向Web应用程序添加登录会话的简单方法,几乎​​不需要其他代码。 I have found that the session timeout for an authentication provider can be configured, for example: 我发现可以配置身份验证提供程序的会话超时,例如:

new CredentialsAuthProvider { SessionExpiry = TimeSpan.FromMinutes(10) }

This provides an expiry from the point of login. 这提供了从登录点到期的到期日。 If we are developing a secure system that must log a user out after a short time then we would change this from the default of 2 weeks to something like the example above. 如果我们正在开发一个必须在短时间内将用户注销的安全系统,那么我们会将其从默认的2周更改为类似上面的示例。 But this has the problem that 10 minutes from logging in the user will be kicked out regardless of whether or not they are still interacting with the application. 但是这有一个问题,即无论用户是否仍在与应用程序进行交互,登录用户的10分钟将被踢出。

Is there a simple way to tell the session provider to extend the expiry time when services are called? 是否有一种简单的方法可以告诉会话提供者延长调用服务时的到期时间?

Ideally it would allow us to extend the session for specific services/requests (so that the session is only extended when the user actively interacts with the application and therefore polled services can be ignored). 理想情况下,它允许我们扩展特定服务/请求的会话(以便仅在用户主动与应用程序交互时扩展会话,因此可以忽略轮询的服务)。

Update 更新

Based on the answer given by mythz we now have a simple solution that provides the level of control we require by extending the ResponseFilterAttribute . 基于mythz给出的答案,我们现在有一个简单的解决方案,通过扩展ResponseFilterAttribute来提供我们所需的控制级别。

ServiceStack doesn't support "sliding session expiration" automatically yet. ServiceStack现在还不支持“滑动会话到期”。 You would basically need to reset the session cache entry on every successful request. 您基本上需要在每次成功请求时重置会话缓存条目。 ie you could have a response filter (since they're only executed for authenticated requests) that re-saves the session that will have the effect of extending it's life-time by the expiry time: 即你可以有一个响应过滤器(因为它们只针对经过身份验证的请求执行),重新保存会话,这会在到期时间内延长它的生命周期:

var userSession = httpReq.GetSession();
httpReq.SaveSession(userSession, slidingExpiryTimeSpan);

If you know what caching provider you're using, eg Redis you can manually update the expiry timeout without re-reading the entry, eg: 如果您知道正在使用的缓存提供程序,例如Redis,您可以手动更新到期超时而无需重新读取条目,例如:

var sessionKey = SessionFeature.GetSessionKey(httpReq.GetSessionId());
redis.ExpireEntryIn(sessionKey, slidingExpiry); //"urn:iauthsession:{sessionId}"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM