在 ASP.Net 中为不同的会话变量设置不同的超时
是否可以为 ASP.Net 中的不同会话设置不同的超时?
已编辑 我的意思是,在同一页面中我有 2 个会话变量 Session["ss1"] 和 Session["ss2"],是否可以为每个会话设置超时?或者有没有办法做同样的事情,比如将会话保存到 cookie 并设置过期时间? 抱歉,我刚刚接触 ASP.Net
Is this possible to set different timeout for different session in ASP.Net?
Edited
I mean that in the same page i have 2 session variable Session["ss1"] and Session["ss2"], is there possible to set timeout for each session? Or is there anyway to do the same like save session to cookie and set expire?
Sry im just new to ASP.Net
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我编写了一个非常简单的扩展类来执行此操作。您可以在此处找到源代码
。
I wrote a very simple extender class that does that. You can find the source code here
Usage:
登录时任意设置超时时间,可以为不同用户设置不同的超时时间...
Set any timeout at login time, you can set different timeout for different users...
如果您正在谈论不同用户的会话超时,那么
您可以使用
Global.asax
,在此您可以使用Session_Start
事件,在此事件中您可以为不同用户设置不同的会话超时If you are talking about session timeout for different users then
You can use
Global.asax
in this you can useSession_Start
event and in this event you can set session timeout differently for different users答案是否定的,会话超时适用于每个用户的所有会话变量。但是,您可以使用缓存或 cookie,它们都支持单个(每个键)级别的超时。
但请坚持住,这些解决方案并非没有一些重大缺点。如果您使用缓存,您将失去会话提供的隐私;如果您使用 cookie,您将受到文件大小和序列化问题的限制。
解决此问题的一种方法是使用缓存并确保在使用的每个密钥中包含用户的会话 ID。这样,您最终将得到一个模仿会话本身的缓存存储。
如果您想要更多功能并且不想费心实现此功能,但是您可以使用 CodePlex 上这个小项目中的 API:
http://www.univar.codeplex.com
2.0 版提供了许多开箱即用的存储类型选项,包括会话绑定缓存。
The answer is no the session timeout applies to ALL session variables per user. You can however use the cache or a cookie which both support timeout on an individua(per key) level.
But hang on those solutions don't come without some major drawbacks. If you use the cache you lose the privacy the session provides and if you use the cookie you are constrained with file size and serialization issues.
One workaround for this is to use the cache and make sure you include the user's session id in every key you use. This way you'll end up with a cache storage that mimics the session itself.
If you want further functionality and don't want to bother about implementing this however you can use the API from this little project on CodePlex:
http://www.univar.codeplex.com
The version 2.0 offers many storage type options out of the box including a session bound cache.
由亚历克斯.首席执行官、创始人 https://www.jitbit.com/ alexblog/196-aspnet-session-caching-expiring-values/
by Alex. CEO, founder https://www.jitbit.com/alexblog/196-aspnet-session-caching-expiring-values/