Creating an ASP.NET Session End Value

I like to create a session value that expires in 5 minutes.

How to do this, do I need to manually compare the creation time when I read it?

Session("sessionval") = myvariable Session("sessioncreated") = now 
+4
source share
7 answers

You cannot define a user timeout for a session variable.

Perhaps you can use the cache for this, with a unique session-specific key.

You can set the timeout for the cached item.

 Cache.Insert("key_dependent_to_session", value, Nothing, DateTime.Now.AddMinutes(5), TimeSpan.Zero) 
+3
source

If you ask how to do this on a single variable, I do not think it is possible, but you can determine the session timeout period using web.config

 <system.web> <sessionState timeout="5" /> <system.web> 

this will affect all of your session objects, not just one.

My suggestion is to provide a custom expiration date for one item - to create a cookie, so you can actually set its expiration value.

 Response.Cookies.Add(New Web.HttpCookie("AdminID", admin.AdminID)) Response.Cookies("AdminID").Expires = Now.AddMinutes(5) 
+6
source

If you want to use SessionState, you can create your own wrapper so that you can cache the wrapper instead of caching the actual item. The following is a brief, unverified shell example that checks if an element is null, or if it has expired, it will call Func, which you can provide to the updated element.

Func accepts the last set value, so if you can determine if the value really remains valid, you can avoid reloading it.

 public class TimeExpirationItem<T> { private T _item=default(T); private TimeSpan _expirationDuration; private Func<T> _getItemFunc; private DateTime _expiresTime; public TimeExpirationItem(TimeSpan expirationDuration, Func<T, T> getItemFunc) { this._getItemFunc = getItemFunc; this._expirationDuration = expirationDuration; } public T Item { get { if (_item == null || ItemIsExpired()) { _item = _getItemFunc(_item); _expiresTime = DateTime.Now.Add(_expirationDuration); } return _item; } } public bool ItemIsExpired() { return DateTime.Now > _expiresTime; } } 

Again, this code is provided without warranty and not verified, but is an example of what you can do.

Using this would look something like this:

Session.Add("ObjectKey",new TimeExpirationItem<MyObject>(new TimeSpan(0,0,5),mo=>MyObjectRepository.GetItemByLogin(Request.User.Identity.Name));

+3
source

It is right. Session state does not have an expiration concept.

If your data is not for every user, you can use Cache. You can even use Cache, but include the username as part of the key, which can give you an expiring session state.

+2
source

I tend to agree with TheTXI.

Although a user-defined timeout cannot be assigned to a single session item, it should be noted that in an InProc session, the SessionStement itself is added as an item to the ASP.NET cache. This allows him to have a timeout.

In your specific scenario, the most appropriate solution is a Cache object.

+1
source

Sorry to resurrect the old question, but I ran into the same problem and had to create an extension class for the HttpSession object.

Here is my blog article about this

PS. code:

 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; } } 
0
source

1) Put this in web.config

 <configuration> <system.web> <sessionState mode="InProc" timeout="5"> </sessionState> </system.web> </configuration> 

2) Set System.Web.SessionState.HttpSessionState.Timeout = 5 minutes.

-1
source

All Articles