locked
Individual session timeout RRS feed

  • Question

  • User1502990794 posted

    Hello frnds

    I want to know is it possible to define indivisual session time out. As I have declared many session variable in global.asax file. Eg:

    Session.Add("Va1", null);
    Session.Add("Va2", null);
    Session.Add("Va3", null);
    Session.Add("Va4", null);



    please let me is it possible or not. If yes so give a suitable example

    Thanks

    Tuesday, October 9, 2012 5:50 AM

Answers

  • User320810878 posted

    Hey ChamindaC can't we do some thing in global.asax or in .aspx file to increase individual session timeout?

    it is possible to change timeout of a given session in global.asax...but if you want to set timeout for each variable only way is to use cache...and if you want to do it for a varible for a user..have your variable key concatanated with session id..

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 10, 2012 12:10 AM

All replies

  • User320810878 posted

    Hi,

     

    look at below answer..i copied from http://stackoverflow.com/questions/6069502/set-different-timeout-for-different-session-in-asp-net

     

     

    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.
    


     

    you can see samples of using cache here...

    http://quickstarts.asp.net/QuickStartv20/aspnet/doc/caching/data.aspx

    Tuesday, October 9, 2012 6:01 AM
  • User1502990794 posted

    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.
    

    Hey ChamindaC can't we do some thing in global.asax or in .aspx file to increase individual session timeout?

    Tuesday, October 9, 2012 6:14 AM
  • User1779161005 posted

    If you're using session to cache values then consider using the ASP.NET data cache API instead -- it supports caching an item and setting an individual timeout. Maybe this is what you were thinking of?

    http://msdn.microsoft.com/en-us/library/ms972379.aspx

    Tuesday, October 9, 2012 9:41 AM
  • User320810878 posted

    Hey ChamindaC can't we do some thing in global.asax or in .aspx file to increase individual session timeout?

    it is possible to change timeout of a given session in global.asax...but if you want to set timeout for each variable only way is to use cache...and if you want to do it for a varible for a user..have your variable key concatanated with session id..

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 10, 2012 12:10 AM
  • User1502990794 posted

    it is possible to change timeout of a given session in global.asax...but if you want to set timeout for each variable only way is to use cache...and if you want to do it for a varible for a user..have your variable key concatanated with session id..

    Ok now I understood. Can you give me suitable example for this?

    Wednesday, October 10, 2012 1:21 AM
  • User320810878 posted

    HI

    Sorry for the delayed response..

     public class Params
        {
            List<KeyValuePair<string, object>> _params;
    
            public Params()
            {
                _params = new List<KeyValuePair<string, object>>();
            }
    
            public T GetValue<T>(string key)
            {
                return (T)_params.First(x => x.Key == key).Value;
            }
            public void Add(string key, string value, Type type)
            {
                _params.Add(new KeyValuePair<string, object>(key, Convert.ChangeType(value, type)));
            }
        }

    You can use the above class to keep you params in cache, you can use the sessionid as the key

    public static Params GetParams(string sessionId)
            {
    
                Params objParams = (Params)System.Web.HttpContext.Current.Cache[sessionId];
                //if not in cache
                if (objParams == null)
                {
                    objParams =  <YourBusinessLogicComponent>.GetAllParameters(sessionId);//you have to implement this method "GetAllParameters(sessionId)"
    
                    
    
                    System.Web.HttpContext.Current.Cache.Add(sessionId, objParams,null,Cache.NoAbsoluteExpiration,new TimeSpan(0,20,0),CacheItemPriority.Normal,null );//this will have a sliding expiration of 20 minutes
                }
                return objParams;
            }

    hope above is helpful to you

    Thursday, October 18, 2012 5:57 AM