Log the user out via the Session_End event inside the Global.asax file (or session timeout)

Source: Internet
Author: User

Session.Abandon () and timeout will trigger the Global.asax Session_End event. This event can be used to record the user exit or session timeout, so that each user will have a login and exit record.

To exit the login call method:

public void Performlogout () {    httpcontext.current.session["performlogout"] = true;    HttpContext.Current.Session.Abandon ();}

Session_End Event code:

protected void Session_End (Object sender, EventArgs e) {    Businesscontext bizcontext = (businesscontext) session[" Businesscontext "];    String LoginID = String. IsNullOrEmpty (Bizcontext.loginid)? "": Bizcontext.loginid;    string title = "Timeout";    if (Convert.toboolean (session["Performlogout"))    {        title = "Logout";    }    BusinessEvent.Log (bizlogcategory.security, bizlogmodule.user_authentication, title, "[Performlogout]logout Successfully "," LoginID: "+ LoginID, Bizcontext);}

There are several points to note:

1. Although we call Session.Abandon () First, we can continue to access all session values in the Session_End event. Because of this, we can assign a value to session["Performlogout" in the Performlogout method, and then use this value to determine whether the Session_End event is triggered by the user logout or by session timeout.

2. ASP. NET inside the session and HttpContext.Current.Session objects are pointed to System.Web.SessionState.HttpSessionState, most of the two objects can be used interchangeably, but in the session In the _end event, HttpContext.Current returns NULL, so the session value can only be accessed through the Session object. Why write like this, refer to here

3. Causes the session timeout setting to be more, has tested has the sessionstate timeout inside the Web. config, IIS application pools's idle time-out, IIS application Pools Recycle, Stop website, modify Web. config, etc.

Https://msdn.microsoft.com/en-us/library/system.web.sessionstate.sessionstatemodule.end (v=vs.110). aspx
Https://msdn.microsoft.com/en-us/library/system.web.sessionstate.httpsessionstate.abandon (v=vs.110). aspx
Http://forums.asp.net/t/1275683.aspx?Can+t+access+to+Session+variable+inside+Session_End+Event
Http://stackoverflow.com/questions/940742/difference-between-session-and-httpcontext-current-session
Http://stackoverflow.com/questions/27657773/why-is-httpcontext-current-null-during-the-session-end-event
Http://stackoverflow.com/questions/19509672/why-is-httpcontext-current-null
Http://stackoverflow.com/questions/12294532/asp-net-values-of-session-variables-in-session-end-event
Https://msdn.microsoft.com/en-us/library/system.web.sessionstate.httpsessionstate.abandon.aspx
Https://msdn.microsoft.com/en-us/library/system.web.sessionstate.sessionstatemodule.end.aspx
Http://stackoverflow.com/questions/13264212/on-session-timeout-capture-info
Http://www.beansoftware.com/ASP.NET-Tutorials/Find-If-Session-Expired.aspx

Log the user out via the Session_End event inside the Global.asax file (or session timeout)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.