Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here.

Session-State Events

ASP.NET provides two events that help you manage user sessions: the Session_OnStart event, which is raised when a new session begins, and the Session_OnEnd event, which is raised when a session is abandoned or expires.


If the Global.asax file or Web.config file for an ASP.NET application is modified, the application will be restarted. If the current session-state mode is InProc, any values stored in application state or session state will be lost. Be aware that some anti-virus software can update the last-modified date and time of the Global.asax or Web.config file for an application. For information on setting the session state mode, see Session-State Modes.

The Session_OnStart Event

You can handle the Session_OnStart event by adding a subroutine named Session_OnStart to the Global.asax file. The Session_OnStart subroutine is run at the beginning of a request if the request begins a new session. A new session will be started if a request is made that does not contain a SessionID value or if the SessionID property contained in the request references a session that has expired.

You can use the Session_OnStart event to initialize session variables as well as to track session-related information.

The Session_OnEnd Event

You can handle the Session_OnEnd event by adding a subroutine named Session_OnEnd to the Global.asax file. The Session_OnEnd subroutine is run when the Abandon method has been called or when the session has expired. A session expires when the number of minutes specified by the Timeout property passes without a request being made for the session.

The Session_OnEnd event is supported only when the session state Mode property is set to InProc, which is the default. If the session state Mode is StateServer or SQLServer, then the Session_OnEnd event in the Global.asax file is ignored. If the session state Mode is set to Custom, then support for the Session_OnEnd event is determined by the custom session-state store provider.

You can use the Session_OnEnd event to clean up session-related information such as information for a user that is tracked in a data source by the SessionID value.

Session Events Example

The following code example shows a sample of the Session_OnStart and Session_OnEnd subroutines you can add to the Global.asax file. The subroutines defined in this example create a counter that keeps track of the number of application users actively using the application. Note that this example will function properly only when the session state Mode property is set to InProc, as the Session_OnEnd event is only supported for in-process session-state storage.

<script language="C#" runat="server">
public void Application_OnStart()
  Application["UsersOnline"] = 0;

public void Session_OnStart()
  Application["UsersOnline"] = (int)Application["UsersOnline"] + 1;

public void Session_OnEnd()
  Application["UsersOnline"] = (int)Application["UsersOnline"] - 1;

See Also

Community Additions

© 2015 Microsoft