0

This answer seems pretty comprehensive but my app is using ApplicationSignInManager rather than AuthenticationManager which the answer uses, so it doesn't really apply to my app.

I'm trying to track my user logins (this is a proprietary website to be used internally by the client). So the client basically wants the login to work like a time card so the boss can see when staff are coming in to the office.

This functionality means I can't rely on persistent logins per ASP.NET Identity default behavior since it doesn't allow me to control if/when the login cookie expires.

What I'd like to do is to expire the cookie at 4am every day regardless of when the login occurs.

For that reason, limiting the cookie's lifespan in configs doesn't really satisfy my requirement either.

Here's my login code - pretty much standard stuff. Per the first answer, I hoped I could embed the expiry of the cookie into the SignIn method but since I'm using not using the AuthenticationManager for sign in, there doesn't seem to be a way to do this with my current code.

var manager = Context.GetOwinContext().GetUserManager<UserManager>();
var signinManager = Context.GetOwinContext().GetUserManager<ApplicationSignInManager>();

// Note that this always persists the login cookie.
var result = signinManager.PasswordSignIn(Email.Text, Password.Text, true, shouldLockout: false);

// Evaluation of result below

If I need to change the way I'm doing logins here, that's fine; I'll just need some guidance on what changes to make (example code would be helpful).

Thanks in advance!

Ortund
  • 8,095
  • 18
  • 71
  • 139

1 Answers1

0

Since nobody bothered to contribute to my question, I went around trying a bunch of stuff and came to my own answer:

Seems like limiting the cookie's lifespan in configs per the link in my question is actually the way to go.

In the auth config method, I determine how many hours there are between now and 4am tomorrow, then I set the cookie's ExpireTimeSpan to that.

public void ConfigureAuth(IAppBuilder app)
{
    var expiryDate = DateTime.Now.Date.AddDays(1).AddHours(4);
    var ts = expiryDate - DateTime.Now;
    app.UseCookieAuthentication(new CookieAuthenticationOptions
    {
        AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
        ExpireTimeSpan = TimeSpan.FromHours(ts.Hours),
        LoginPath = new PathString("/Account/Login"),
        Provider = new CookieAuthenticationProvider
        {
            OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<UserManager, ApplicationUser>(
                validateInterval: TimeSpan.FromMinutes(30),
                regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
        }
    });
}
Ortund
  • 8,095
  • 18
  • 71
  • 139