Supergnuen
Supergnuen

Reputation: 53

How can I detect when a user logs in with Microsoft.AspNet.Identity

I have an .net mvc5 project which uses Microsoft.AspNet.Identity for authentication.

I want to detect/log when a user logs in to the application, both when the user has a "remember me" cookie, and if the user logs in manually.

I have already tried creating a custom AuthenticateFilter based on IAuthenticationFilter but both OnAuthentication and OnAuthenticationChallenge fires several times for every request.

I know I can add the code in the login action, but I also want to catch the users that are logging in with a cookie.

Is there anywhere else in the Identity code, I can override, and add my logging code?

Upvotes: 3

Views: 1416

Answers (2)

Peter Hurtony
Peter Hurtony

Reputation: 470

There are some hook-s for this in the MVC's architecture. Implement one of these in Global.asax:

  • protected void Application_AuthenticateRequest() { }
  • protected void Application_PostAuthenticateRequest() { }
  • protected void Application_AuthorizeRequest() { }

Upvotes: 1

Hao Kung
Hao Kung

Reputation: 28200

You can probably do this inside of ValidateIdentity on the Cookie middleware, since that will be called even if remember me is set.

Upvotes: 1

Related Questions