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

发布于 2024-11-08 14:48:40 字数 190 浏览 4 评论 0原文

是否可以为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

时光与爱终年不遇 2024-11-15 14:48:40

我编写了一个非常简单的扩展类来执行此操作。您可以在此处找到源代码

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

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));
意犹 2024-11-15 14:48:40

登录时任意设置超时时间,可以为不同用户设置不同的超时时间...

HttpContext.Current.Session.Timeout = 540;

Set any timeout at login time, you can set different timeout for different users...

HttpContext.Current.Session.Timeout = 540;
怀念你的温柔 2024-11-15 14:48:40

如果您正在谈论不同用户的会话超时,那么
您可以使用 Global.asax ,在此您可以使用 Session_Start 事件,在此事件中您可以为不同用户设置不同的会话超时

If you are talking about session timeout for different users then
You can use Global.asax in this you can use Session_Start event and in this event you can set session timeout differently for different users

诗酒趁年少 2024-11-15 14:48:40

答案是否定的,会话超时适用于每个用户的所有会话变量。但是,您可以使用缓存或 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.

风蛊 2024-11-15 14:48:40
/// <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");

由亚历克斯.首席执行官、创始人 https://www.jitbit.com/ alexblog/196-aspnet-session-caching-expiring-values/

/// <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/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文