Benjamin E.
Benjamin E.

Reputation: 5162

Changeable cookie expiry time

By default, the Identity 2.1 authentication cookie expiry time is set globally in Startup.Auth.cs via CookieAuthenticationOptions. Is there a way to do that on-the-fly, depending on the user, so it's individually configurable?

Upvotes: 0

Views: 188

Answers (1)

Yes, it is possible. Use AuthenticationProperties to specify the expiry at the time you call SignIn, like this.

var claims = new List<Claim>() { new Claim(ClaimTypes.Name, "Alice") };
var identity = new ClaimsIdentity(claims, "ApplicationCookie");

var properties = new AuthenticationProperties()
{
    ExpiresUtc = DateTimeOffset.Now.AddDays(1) // One day expiry for Alice
};

Request.GetOwinContext().Authentication.SignIn(properties, identity);

Upvotes: 2

Related Questions