Set different timeout for different session variables in ASP.Net
Asked Answered
L

5

5

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

Leticialetisha answered 20/5, 2011 at 8:47 Comment(4)
more information is needed to understand your questionPardew
what is your meaning different?Pardew
Did you checked: forums.asp.net/t/1563991.aspx/1 ?Dap
Its not possible in current .Net Framework versions, But Microsoft should think about this in next version :)Arboreous
E
6

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));
Ermaermanno answered 16/12, 2013 at 0:18 Comment(0)
I
3

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

HttpContext.Current.Session.Timeout = 540;
Inactive answered 20/5, 2011 at 9:7 Comment(1)
KamalaPrakash it I set session timeout in this way. won't it look in web.config any more?Emblematize
A
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.

Abbottson answered 20/5, 2011 at 8:48 Comment(0)
P
0

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

Pardew answered 20/5, 2011 at 9:6 Comment(0)
F
0
/// <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/

Farhi answered 22/2, 2017 at 13:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.