Search code examples
asp.netasp.net-mvcauthenticationowinkatana

Detect Login in Owin for updating Database


I am using Owin for authentication in my ASP.net MVC application:

app.UseApplicationSignInCookie();
app.UseFormsAuthentication(new FormsAuthenticationOptions()
{
    AuthenticationMode = Microsoft.Owin.Security.AuthenticationMode.Active,
    ExpireTimeSpan = 64800 /* cookie valid for 48 Days */
});

In my database I store the users last login date and I use this to send mails to inactive users. If the user manually does a login on my site, I can easily update the database. But if the user has the 'remember me' Cookie set, Owin automatically re-authenticates the user and I never get signaled that this user did just start a new session.

Is there some sort of event which Owin fires as soon it has successfully authenticated a user in any way? I would expect something like that sample:

app.OnUserAuthenticated += () => {
    var user = HttpContext.Current.User;
    /* hooray i am authenticated */
};

Solution

  • As I'm not into OWIN yet I will suggest something else. You could hook up some code on the Session_Start event and check if the user is authenticated there. If the user is authenticated you can go ahead and update the information in your DB.