سؤال

I have an ASP.Net MVC3 app. When the LogIn action is called, I use the MembershipProvider to validate the user and FormsAuthentication to set the cookie.

Additionally, I get some info about the user from a database and store it in Session.

When the user subsequently visits the site, they're already authenticated via the cookie, and I'm looking for somewhere to hook into so I can fetch the info about the user from the database again.

Is HttpApplication.AuthorizeRequest() the best place to do this? Obviously this is called for every request so I was hoping there was something I could use that just indicated the user had been authenticated - either explicitly after logging in or when they're authenticated automatically.

هل كانت مفيدة؟

المحلول

There are several events that get triggered on every request, HttpApplication.AuthorizeRequest() should work.

In order to only fetch from the database for logged in users, you can check the Name property of User.Identity which only gets set once the user authenticates:

if(!string.IsEmpty(User.Identity.Name))
{
 //make call to database
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top