1

hi there I want to store the login date and logout date for each user

for statistics purposes :

login is something so easy but what about the logout

which is the best event or something that let us know the logout regardless the logout button which is also so easy .

I mean how to know that session finished in asp.net mvc .

tereško
  • 58,060
  • 25
  • 98
  • 150
Feras Taleb
  • 688
  • 1
  • 4
  • 14

2 Answers2

0

You can use this code within Global.asax to detect when a session has ended (and perform some kind of action).

protected void Session_End(Object sender, EventArgs e)
{
    // Perform action here
}
Matthew
  • 777
  • 1
  • 9
  • 23
0

Each time a new session is instantiated, you could store the session id and login date in a database along with a null logout date field. If logout is explicit, you can fill this in. On regular intervals, you can execute batches against the database to record a "logout" for any items older than your session timeout that have null logout values.

Edit: Just as a side note, you'd probably also need a "last activity" field to make sure you aren't recording a logout for a currently active session.

Joel Etherton
  • 37,325
  • 10
  • 89
  • 104
  • so there is nothing event Session time out like in Regular asp.net global asax ??? – Feras Taleb Jan 03 '11 at 13:23
  • Can you tell me about how to excute batches on regular intervals ?? – Feras Taleb Jan 03 '11 at 13:57
  • @feras: I typically use the task scheduler under the control panels. I write small windows forms controls that can run with/without interactivity that are scheduled with various command line parameters to perform these tasks. You could probably use a windows service, but I think a small console or winforms application would be most suitable. – Joel Etherton Jan 03 '11 at 14:26
  • excuse me sir but The application is in shared host not in my own server so I can only use my asp.net application . don't you think making a timer object for each session is a good idea or is it a waste of performance ,,, any other ideas ? – Feras Taleb Jan 03 '11 at 14:48
  • @feras - A timer wouldn't be a very good idea because of the overhead it would add (timers are expensive constructs), but you could also achieve a similar result if you keep a static application variable that holds a counter or last-attempted date and then use that to perform an action in the `Application_BeginRequest` method of global.asax. Here is also a link with some other ideas for similar solutions: http://stackoverflow.com/questions/542804/asp-netbest-way-to-run-scheduled-tasks – Joel Etherton Jan 03 '11 at 15:12
  • I read a solution about that could you tell me your opinion :http://www.tyronedavisjr.com/index.php/2008/11/23/detecting-session-timeouts-using-a-aspnet-mvc-action-filter/ – Feras Taleb Jan 04 '11 at 08:19