1

I want to ask that can i logout user in Application_End??? ,I know when Application_End invokes Application_End global.asax

But i just want to know that is it right to make all users logout in Application End, I just want to enter logout date time of user.

    void Application_End(object sender, EventArgs e)
    {
        try
        {
            var LisLoginUsers = db.USER_LOGIN.Where(z => z.LOGOUT_DATETIME == null).ToList();
            if (LisLoginUsers.Count != 0)
            {
                for (int i = 0; i < LisLoginUsers.Count; i++)
                {
                    LisLoginUsers[i].LOGOUT_DATETIME = System.DateTime.Now;
                    db.SaveChanges();

                }
            }
        }
        catch (Exception msg)
        {
            ExceptionLogging.SendErrorToText(msg);
            Response.Redirect("/Account/Error/");

        }
    }
Community
  • 1
  • 1
Mussaib Siddiqui
  • 199
  • 2
  • 15

1 Answers1

0

No, you can't use Application_End to record singout in real sites.

Since Application_End event is not tied to any request there are some things you can't do - you can't act on particular user or redirect response somewhere (as there is no request/response to start with).

Indeed you can wipe out or bulk update information in the DB, but it will not achieve your actual goals to "enter logout date time of user".

Application shutdown is not tied to any user behavior - it only relates to stopping server side code for whatever reason (i.e. restarting the site due to update or inactivity). From user's point of view they still may be looking at pages and consider themselves "logged in".

In case you have non-trivial site with more than one server there is also no correlation when each server restarts IIS process making Application_End completely unfit for "user logged out" as one server can actively serve requests while other restarting.

Your best bet is to timeout user sessions either explicitly (i.e. 40 minutes from signing in) or with sliding expiration by either updating time on every request / heartbeat AJAX pings from the page.

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179