0

I have to maintain the user login and logout time in database. For login its easy but for logout , if I use Application_End () in global.asax for logging. It gives me exception.

My Code in asax file:

Application_End()
{
  HttpCurrent.Context.Session.Abandon();
  LogUserLogoutTimeToDatabase();
}
samjudson
  • 56,243
  • 7
  • 59
  • 69
  • What exception does it give you? This is probably an indicator as to what the problem is. – samjudson Dec 24 '12 at 12:20
  • Does logout have a specific action associated with it ? An application end probably means all your users lose their sessions, this is not the only way for a log out, i am guessing. – Ravi Y Dec 24 '12 at 12:21
  • Something like Session is not valid for this context – user1926635 Dec 24 '12 at 12:27
  • @ryadavilli i want logout log for individual users. Not for all users at a time – user1926635 Dec 24 '12 at 12:28
  • Which is the reason why i said Application end is not the right place. Check the answers below, people have given good explanations about using a log off button and handling the session end event. – Ravi Y Dec 24 '12 at 12:30
  • I just want to say to say log the user as logout in db if he 1. Closes his/hr browser 2. Properly logout using the logout button. 3. Or somehow he got disconnected from the server (e.g. power failure and immediate shutdowns) – user1926635 Dec 24 '12 at 12:31

3 Answers3

0

You're best and first bet would be a logout button in the application. After that you can use a Session timeout event with a very short session time.

Application_End is triggered when the application is shutting down but not during a server crash.

CodingBarfield
  • 3,392
  • 2
  • 27
  • 54
0

You will need to use Session_End(). A user can simply close their browser, and to them, they are now "logged off" - but nothing has alerted the server that they have logged off unless they clicked a "log off" button before closing their browser. So, once their session times out, the Session_End() event will be triggered and you can capture your logout in there.

M.Ob
  • 1,785
  • 14
  • 27
0

the best way to achieve this is , use onClick event of logout button.

when user click on log out button , store that time in database...

punter
  • 460
  • 1
  • 6
  • 22