Although technically being logged in is the same as being authenticated, I have a different mental model of this.
In my mind the three following things are separate issues:
- User has/gets a session
- User is authenticated
- User is logged in
For me the last one of these means: "A session has been created for the user, the user is authenticated and the session has been initialized for the authenticated user".
Using this model, a user can become logged in when:
- The user logs in on the login page and the pre-existing session is
initialized with necessary user data
- A pre-authenticated user comes to the site and a new session is
created and initialized for him/her
Similarly, the user becomes logged out when his/her initialized session is destroyed.
Using this model will mean:
- You can identify when a user "logs in" either in the
Login.OnLoggedIn
event the or the Session_Start
event in Global.asax
. Of course, the session start event fires also for unauthenticated users, so you need to verify that the user is authenticated when the event fires.
- You can somewhat reliably tell when a user "logs out", either by explicitly loggin out or when a properly initialized session is destoyed in the
Session_End
event in Global.asax. I say somewhat reliably, because I think the Session_End event(s) will not necessarily be fired when the application pool recycles or dies in a crash. Although I haven't tested this so I might be wrong.
- A user can be simultaneously "logged in" multiple times. At least in IE you can start a "New session" from the File menu. This starts a new IE which is not sharing the session cookies with any preexisting IE windows. This means a new session will be created by the server when the user comes to the site, and depending on the autentication mechanism used it might mean he/she will also have to authenticate again.
It will not let you "list all currently logged in users" out of the box. You will need to create som way of keeping track of that yourself I think. This can me more or less difficult to do. Especially in the case when your application is running in some sort of load balanced environment, getting a list of all current user can be tricky.