Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7808167
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T03:04:11+00:00 2026-06-02T03:04:11+00:00

How can I access session variable from the HttpModule ? I set following session

  • 0

How can I access session variable from the HttpModule?

I set following session variable in .cs page, which I want access in the HttpModule:

Session[“username”] = “BLAH”

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T03:04:13+00:00Added an answer on June 2, 2026 at 3:04 am

    Here is an example of using session within HttpModule, found here:

    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,
    // regradless 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; }
          }
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Following the solution found at Can I access session state from an HTTPModule? ,
Possible Duplicate: Can I access session state from an HTTPModule? I have set up
If I want to set a variable that my whole controller can access, how
Why I can access Session object in Page_Load method in instance of System.Web.UI.Page, but
im trying to establish and access a session from my servlet, but I can't
I am aware that I can access all external links on a page using
I want to disallow access to file system from clients code, so I think
I have a variable I want to access in my application.html.erb so it is
I'm using Oracle APEX but am unsure how to access the following variables from
I can access Spring beans in my Servlets using WebApplicationContext springContext = WebApplicationContextUtils.getWebApplicationContext(getServletContext()); in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.