How to handle Session timeout if same asp.net sessionId is shared across multiple asp.net web application -


how handle session timeout if same asp.net session id shared across multiple asp.net web application.

we have multiple web applications. these web applications running on dnn , sharing session id. session each application created @ time application accessed. want find way though can handle session time out because traditional way (using session_start on globle.aspx check existing session id , new session) not working.

please me know how handle session timeout , don't implement @ page level.

rather using asp.net session state, instead use caching achieve similar result. can use multiple tenant application if each tenant has own sessions. can respond cache timeouts using callback methods, more reliable using session state events.

the downside won't scale multiple servers unless come distributed cache solution. use azure distributed caching resolve this, or there other options.

here session caching solution typically like:

public class threadsafecache {        public shared threadsafecache()     {         if (cache == null)         {             cache = system.runtime.caching.memorycache.default;         }         if (synclock == null)         {             synclock = new readerwriterlockslim(lockrecursionpolicy.norecursion);         }     }      private shared system.runtime.caching.objectcache cache;     private shared readerwriterlockslim synclock;      public shared bool contains(string key)     {         synclock.enterreadlock();         try         {             return this.cache.contains(key);         }                 {             synclock.exitreadlock();         }     }      public shared object getoradd(string key, func<object> loadfunction, action<cacheentryremovedarguments> callbackfunction)     {         // or add item cache         object item = null;          synclock.enterreadlock();         try         {             item = cache.get(key);         }                 {             synclock.exitreadlock();         }          if (item == null)         {             synclock.enterwritelock();             try             {                 // lazy lock pattern - need check again after                 // lock ensure 1 thread makes through                 if (item == null)                 {                     // item                     item = loadfunction();                      var policy = new cacheitempolicy();                      // set cache expiration (from last access).                     policy.slidingexpiration = timespan.fromminutes(30);                      // setting priority not removable ensures                      // app pool recycle doesn't unload item, timeout will.                     policy.priority = cacheitempriority.notremovable;                      // setup expiration callback.                     policy.removedcallback = callbackfunction;                      cache.add(key, item, policy);                 }             }                         {                 synclock.exitwritelock();             }         }          return item;     }      public shared void remove(string key)     {         synclock.enterwritelock();         try         {             this.cache.remove(key);         }                 {             synclock.exitwritelock();         }     } } 

then use like:

var sessionid = "1234"; // string cookie or string stored in asp.net session state var tenantid = "2"; // identifier specific tenant within application var key = tenantid + "_" + sessionid;  threadsafecache.getoradd(key, loaditem, cacheitemremoved);  private object loaditem() {      // todo: load item (from wherever need load from)      return item; }  private void cacheitemremoved(cacheentryremovedarguments arguments) {     // respond here when cache expires } 

the trick ensure cache key built of both user's session id (from cookie) , id of application.

note haven't tested this, may need tweaking working.


Comments

Popular posts from this blog

c++ - QTextObjectInterface with Qml TextEdit (QQuickTextEdit) -

javascript - angular ng-required radio button not toggling required off in firefox 33, OK in chrome -

xcode - Swift Playground - Files are not readable -