How to create a new session in ASP.NET programmatically

If you want to create a new session (open a new window in a new session), without disturbing/loosing the other one, you will have to use the SessionIDManager.
Here is a short example (this will only work with <sessionState cookieless=”true” /> in the web.config):

SessionIDManager manager = new SessionIDManager();
string newId = manager.CreateSessionID(HttpContext.Current);

string urlOrig = HttpContext.Current.Request.UrlReferrer.ToString();
string urlNew = urlOrig;
string[] ss = urlOrig.Split(new Char[] { '/' });

urlNew = urlOrig.Replace(ss[4], "(S(" + newId + "))"); //the string "(S(" is necessary to identify the sessionID (ss[4])
urlNew = urlNew.Replace(ss[6], "frmStart.aspx");
HyperLink.NavigateUrl = urlNew;

Normally you should save the new sessionID to the context, but we have got an expection while executing the save method. So we only saved the new sessionID to the NavigateUrl of the HyperLink.If the user clicks on the HyperLink he will got a completely new session in a new window (target=”_blank”). Thus it’s possible to switch between the windows/sessions.

Be aware: In our application (MA14) we cannot use cookieless=true because the LoadBalancer cannot deal with this configuration.

Helpful Links:

Related Posts

No related posts.

Leave a Reply