A wierd ASP.Net bug or is there a better explanation?
I think I have come across a wierd bug in the way ASP.Net Session State works within HttpModules. I know this sounds implausible, but I can't figure out any other explanation.
More often than not, the following HttpModule throws an NullReferenceException when I try to access httpContext.Session in the PreRequestHandlerExecute handler:
public class AuthenticationModule : IHttpModule {
HttpContext httpContext;
public void Init(HttpApplication context) {
httpContext = context.Context;
context.PreRequestHandlerExecute += new EventHandler(context_PreRequestHandlerExecute);
}
public void Dispose() {
}
private void context_PreRequestHandlerExecute(object sender, EventArgs e) {
if(httpContext.Request.IsAuthenticated) {
if(httpContext.Session["User"] == null) {
// the object in the session has been lost, go back to
// the database and rebuild it and put it back in the session
// ...
}
}
}
}
This is pretty bizarre behaviour - Session State is definitely enabled across the application.