Question

The code sample at http://support.microsoft.com/kb/2527105 is exactly what I need to share sessions between two subdomains. Only problem is it doesn't work in a real life situation. It works fine when the ony file being requested is the page itself, but throws an error 'session state is not available in this context' when other files are part of the request, such as if I add stylesheets or javascript files to the page. The code is generating this error in the "if (context.Session != null &&" line below:

void context_PostRequestHandlerExecute(object sender, EventArgs e)
{
    HttpApplication context = (HttpApplication)sender;
    HttpCookie cookie = context.Response.Cookies["ASP.NET_SessionId"];

    if (context.Session != null &&
        !string.IsNullOrEmpty(context.Session.SessionID))
    {
        cookie.Value = context.Session.SessionID;
        if (rootDomain != "localhost")
        {
            cookie.Domain = rootDomain;
        }
        cookie.Path = "/";
    }
}
Was it helpful?

Solution

You can try a Try Catch Block, then you can trap for a null value.

    private HttpSessionState GetSession(HttpApplication context)
    {
        try
        {
            //On Abadon or Logout this returns "Session state is not available in this context. "
            return context.Session;
        }
        catch (HttpException)
        {
            return null;
        }
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top