Auto Ad Code

Tuesday, July 6, 2010

Subdomains and Session expiry issue

Many of us use the subdomains in our website. This is pretty simple but things get hectic when we see Session variables are lost while navigating in between Subdomains.
How to overcome this?
Okay here is the solution..
Add following Page_Unload function in every page from where you are navigating to any other Subdomain

Protected Sub Page_Unload(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Unload
            Context.Response.Cookies.Item("ASP.NET_SessionId").Domain = "[YOUR WEBSITE]"
            Dim sessionCookie As New Web.HttpCookie("ASP.NET_SessionId", Context.Session.SessionID)
            sessionCookie.Domain = "[YOUR WEBSITE]"
            Context.Response.SetCookie(sessionCookie)
End Sub


Where [YOUR WEBSITE] should be in form = .myweb.com i.e. dot yourwebsite dot com/net/org/co.in/co.uk etc

1 comment: