2006-10-08

Accessing Session in HttpApplication

This is an interesting thing, when trying to access the Session object in your global class like this:

public class global : System.Web.HttpApplication {

    void Application_PreRequestHandlerExecute(object sender, EventArgs e) {

        if(User.Identity.IsAuthenticated) {
            if(Session != null) {

            }

            if(Context.Session != null) {

            }
        }
    }
}


The first access to the Session throws this exception:

Session state is not available in this context.

But the second one succeeds, i assume that they would both be accessing the same underlying object. Weird.