简体   繁体   English

在ASP.Net中为不同的会话变量设置不同的超时

[英]Set different timeout for different session variables in ASP.Net

Is this possible to set different timeout for different session in ASP.Net? 这是否可以为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? 编辑我的意思是在同一页面我有2个会话变量Session [“ss1”]和Session [“ss2”],是否有可能为每个会话设置超时? Or is there anyway to do the same like save session to cookie and set expire? 或者无论如何都要像保存会话到cookie并设置过期一样? Sry im just new to ASP.Net 我刚刚接触ASP.Net新手

I wrote a very simple extender class that does that. 我写了一个非常简单的扩展类来做到这一点。 You can find the source code here 你可以在这里找到源代码

Usage: 用法:

//store and expire after 5 minutes
Session.AddWithTimeout("key", "value", TimeSpan.FromMinutes(5));

在登录时设置任何超时,您可以为不同的用户设置不同的超时...

HttpContext.Current.Session.Timeout = 540;
/// <summary>
/// this class saves something to the Session object
/// but with an EXPIRATION TIMEOUT
/// (just like the ASP.NET Cache)
/// (c) Jitbit 2011. MIT license
/// usage sample:
///  Session.AddWithTimeout(
///   "key",
///   "value",
///   TimeSpan.FromMinutes(5));
/// </summary>
public static class SessionExtender
{
  public static void AddWithTimeout(
    this HttpSessionState session,
    string name,
    object value,
    TimeSpan expireAfter)
  {
    session[name] = value;
    session[name + "ExpDate"] = DateTime.Now.Add(expireAfter);
  }

  public static object GetWithTimeout(
    this HttpSessionState session,
    string name)
  {
    object value = session[name];
    if (value == null) return null;

    DateTime? expDate = session[name + "ExpDate"] as DateTime?;
    if (expDate == null) return null;

    if (expDate < DateTime.Now)
    {
      session.Remove(name);
      session.Remove(name + "ExpDate");
      return null;
    }

    return value;
  }
}
Usage:

//store and expire after 5 minutes
Session.AddWithTimeout("key", "value", TimeSpan.FromMinutes(5));

//get the stored value
Session.GetWithTimeout("key");

by Alex. 亚历克斯 CEO, founder https://www.jitbit.com/alexblog/196-aspnet-session-caching-expiring-values/ CEO,创始人https://www.jitbit.com/alexblog/196-aspnet-session-caching-expiring-values/

如果您正在讨论不同用户的会话超时,那么您可以使用Global.asax ,您可以使用Session_Start事件,在这种情况下,您可以为不同的用户设置不同的会话超时

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. 但是,您可以使用缓存或cookie,它们都支持单个(每个键)级别的超时。

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. 如果使用缓存,则会丢失会话提供的隐私,如果使用cookie,则会受到文件大小和序列化问题的限制。

One workaround for this is to use the cache and make sure you include the user's session id in every key you use. 一种解决方法是使用缓存并确保在您使用的每个密钥中包含用户的会话ID。 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: 如果您想要进一步的功能并且不想打扰实现它,那么您可以使用CodePlex上这个小项目的API:

http://www.univar.codeplex.com http://www.univar.codeplex.com

The version 2.0 offers many storage type options out of the box including a session bound cache. 2.0版本提供了许多开箱即用的存储类型选项,包括会话绑定缓存。

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

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