Is there a way to be notified when a user becomes logged in with an ASP.net website?
Note: A user can become logged in without visiting a "login page". If the "remember me" cookie exists, they can hit an arbitrary page and be logged in.
When a user is logged in i want to fetch some session related information.
Note: There is the
Login.LoggedIn
event. Problem is that that control doesn't exist on every page; and the one page it is present on (Login.aspx
) doesn't callOnLoggedIn
event.
In the same way that Global.asax
has a global On Session Start notification:
void Session_Start(object sender, EventArgs e)
{
}
i assume somewhere there's a On User Logged In notifiation:
void LoggedIn(object sender, EventArgs e)
{
}
Bonus Reading
- OnLoggedIn event on Login page ASP.NET
- Run custom code on login
- MDSN Logon.OnLoggedIn event
- How to update last login date if “Remember Me” set?
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:
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:
Similarly, the user becomes logged out when his/her initialized session is destroyed.
Using this model will mean:
Login.OnLoggedIn
event the or theSession_Start
event inGlobal.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.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.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.
You can call your code in those two places:
OnLoggedIn
event from theLogin
control and also when the session starts (using theSession_Start
event in your Global.asax), as this will be the first request with the user data. There you can check if the user is logged and if so, do what you need.You can make the check on
Application_AuthenticateRequest
on global.asax, is the place that you can check if the request is logged in or not together with your session data, and decide if session data needs initialization as you say.Or the same results with
I think you don't have a unique place to do that. In my case (MVC + log4net) I use this:
In
Global.asax
I check for authenticated users with pre-existing cookie.In my Account controller I check for local logins (I'm using Internet Application Template from MVC4, but you can do this in your
Login.OnLoggedIn
if you're using Web forms)But I need to check for OAuth logins too, like this: