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

  • Home
  • SEARCH
  • 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 3236432
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T17:37:17+00:00 2026-05-17T17:37:17+00:00

In my asp.net website I am creating a session upon user login and I

  • 0

In my asp.net website I am creating a session upon user login and I would like to perform some operations in the database just before this session will expire.I am having problem in determining where should I write code and how will I know the session is going to expire.

I am not sure if ‘session_end’ event of ‘Global.asax’ suits my requirements as the session I want to check is created manually(not a browser instance).

Could someone please put me in the right direction?

Thanks.

  • 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-05-17T17:37:17+00:00Added an answer on May 17, 2026 at 5:37 pm

    This could be pretty tricky, namely because the Session_End method is only supported when the Session mode is set to InProc. What you could do, is use an IHttpModule that monitors an item stored in session, and fires an event when the Session expires. There is an example over on CodeProject (http://www.codeproject.com/KB/aspnet/SessionEndStatePersister.aspx), but its not without limitations, for instance it doesn’t work in webfarm scenarios.

    Using Munsifali’s technique, you could do:

    <httpModules>
     <add name="SessionEndModule" type="SessionTestWebApp.Components.SessionEndModule, SessionTestWebApp"/>
    </httpModules>
    

    And then wire up the module at the application start:

    protected void Application_Start(object sender, EventArgs e)
    {
      // In our sample application, we want to use the value of Session["UserEmail"] when our session ends
      SessionEndModule.SessionObjectKey = "UserEmail";
    
      // Wire up the static 'SessionEnd' event handler
      SessionEndModule.SessionEnd += new SessionEndEventHandler(SessionTimoutModule_SessionEnd);
    }
    
    private static void SessionTimoutModule_SessionEnd(object sender, SessionEndedEventArgs e)
    {
       Debug.WriteLine("SessionTimoutModule_SessionEnd : SessionId : " + e.SessionId);
    
       // This will be the value in the session for the key specified in Application_Start
       // In this demonstration, we've set this to 'UserEmail', so it will be the value of Session["UserEmail"]
       object sessionObject = e.SessionObject;
    
       string val = (sessionObject == null) ? "[null]" : sessionObject.ToString();
       Debug.WriteLine("Returned value: " + val);
    }
    

    Then, when the Session starts, you can throw in some user data:

    protected void Session_Start(object sender, EventArgs e)
    {
       Debug.WriteLine("Session started: " + Session.SessionID);
    
       Session["UserId"] = new Random().Next(1, 100);
       Session["UserEmail"] = new Random().Next(100, 1000).ToString() + "@domain.com";
    
       Debug.WriteLine("UserId: " + Session["UserId"].ToString() + ", UserEmail: " + 
                     Session["UserEmail"].ToString());
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am creating a website on ASP.Net in which a user logs on and
I have an existing Asp.Net MVC Website and I would also like to provide
I am creating an installer for an ASP.Net website using WiX. How do you
I have an asp.net website that allows the user to download largish files -
I'm creating an ASP.net website which handles thousands of requests and it all stems
I am new to ASP.NET. I am creating a website in which i have
I'm creating a new ASP.net website via Visual Studio. I then try to run
I'm creating a new ASP.net website via Visual Studio. I then try to run
I'm working on a ASP.NET website that on some requests will run a very
I am looking into creating a new website using ASP.NET 4.0. I am currently

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.