Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I access session state from an HTTPModule?

I could really do with updating a user's session variables from within my HTTPModule, but from what I can see, it isn't possible.

UPDATE: My code is currently running inside the OnBeginRequest () event handler.

UPDATE: Following advice received so far, I tried adding this to the Init () routine in my HTTPModule:

AddHandler context.PreRequestHandlerExecute, AddressOf OnPreRequestHandlerExecute

But in my OnPreRequestHandlerExecute routine, the session state is still unavailable!

Thanks, and apologies if I'm missing something!

like image 661
Chris Roberts Avatar asked Nov 09 '08 19:11

Chris Roberts


People also ask

How do you use session state?

You can also configure session state by using the EnableSessionState value in the @ Page directive. The sessionState element enables you to specify the following options: The mode in which the session will store data. The way in which session identifier values are sent between the client and the server.

What is session state data?

Session state. Session state is an ASP.NET Core scenario for storage of user data while the user browses a web app. Session state uses a store maintained by the app to persist data across requests from a client. The session data is backed by a cache and considered ephemeral data.

Where is session state stored?

Session state can be stored in one of the following modes: In - Process: Stored in the same ASP.Net Process. State Server: Stored in the some other system. SQL Server: Stored in the SQLServer database.


2 Answers

Found this over on the ASP.NET forums:

using System; using System.Web; using System.Web.Security; using System.Web.SessionState; using System.Diagnostics;  // This code demonstrates how to make session state available in HttpModule, // regardless of requested resource. // author: Tomasz Jastrzebski  public class MyHttpModule : IHttpModule {    public void Init(HttpApplication application)    {       application.PostAcquireRequestState += new EventHandler(Application_PostAcquireRequestState);       application.PostMapRequestHandler += new EventHandler(Application_PostMapRequestHandler);    }     void Application_PostMapRequestHandler(object source, EventArgs e)    {       HttpApplication app = (HttpApplication)source;        if (app.Context.Handler is IReadOnlySessionState || app.Context.Handler is IRequiresSessionState) {          // no need to replace the current handler          return;       }        // swap the current handler       app.Context.Handler = new MyHttpHandler(app.Context.Handler);    }     void Application_PostAcquireRequestState(object source, EventArgs e)    {       HttpApplication app = (HttpApplication)source;        MyHttpHandler resourceHttpHandler = HttpContext.Current.Handler as MyHttpHandler;        if (resourceHttpHandler != null) {          // set the original handler back          HttpContext.Current.Handler = resourceHttpHandler.OriginalHandler;       }        // -> at this point session state should be available        Debug.Assert(app.Session != null, "it did not work :(");    }     public void Dispose()    {     }     // a temp handler used to force the SessionStateModule to load session state    public class MyHttpHandler : IHttpHandler, IRequiresSessionState    {       internal readonly IHttpHandler OriginalHandler;        public MyHttpHandler(IHttpHandler originalHandler)       {          OriginalHandler = originalHandler;       }        public void ProcessRequest(HttpContext context)       {          // do not worry, ProcessRequest() will not be called, but let's be safe          throw new InvalidOperationException("MyHttpHandler cannot process requests.");       }        public bool IsReusable       {          // IsReusable must be set to false since class has a member!          get { return false; }       }    } } 
like image 186
4 revs, 3 users 68% Avatar answered Oct 13 '22 07:10

4 revs, 3 users 68%


HttpContext.Current.Session should Just Work, assuming your HTTP Module isn't handling any pipeline events that occur prior to the session state being initialized...

EDIT, after clarification in comments: when handling the BeginRequest event, the Session object will indeed still be null/Nothing, as it hasn't been initialized by the ASP.NET runtime yet. To work around this, move your handling code to an event that occurs after PostAcquireRequestState -- I like PreRequestHandlerExecute for that myself, as all low-level work is pretty much done at this stage, but you still pre-empt any normal processing.

like image 30
mdb Avatar answered Oct 13 '22 07:10

mdb