2
votes

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 */
};
2

2 Answers

0
votes

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.

0
votes

Why don't you just put middleware after the cookie middleware that looks for OwinContext.Authentication.User != null? This would basically do the same as the event you're looking for.