How to set cookie expiration in ASP.NET MVC 5?

In Loginmy method AccountController, I have the following:

var claims = new List<Claim>();
claims.Add(new Claim(ClaimTypes.Name, model.UserName));
AuthenticationManager.SignIn(
  new AuthenticationProperties { IsPersistent = model.RememberMe,
                                 ExpiresUtc = expiry },
  new ClaimsIdentity(claims, DefaultAuthenticationTypes.ApplicationCookie));

where expiryis the calculated value DateTimeOffset.

No matter what I set for the value expiry, the expiration is .AspNet.ApplicationCookiealways 14 days.

Is there any way to set this expiration value?

Is this related to https://katanaproject.codeplex.com/workitem/115 ?

Please note that Startup.Auth.csI do not have a set of properties ExpireTimeSpan. When I set this property, its value is respected, and the above approach still does nothing.

+4
source share
1

this commit 16 2014 .

    protected override async Task ApplyResponseGrantAsync()
    ...
    DateTimeOffset issuedUtc = Options.SystemClock.UtcNow; 
(-) DateTimeOffset expiresUtc = issuedUtc.Add(Options.ExpireTimeSpan);
    context.Properties.IssuedUtc = issuedUtc; 
(-) context.Properties.ExpiresUtc = expiresUtc;
(+) if (!context.Properties.ExpiresUtc.HasValue)
(+) {
(+)     context.Properties.ExpiresUtc = issuedUtc.Add(Options.ExpireTimeSpan);
(+) } 

    Options.Provider.ResponseSignIn(context); 

    if (context.Properties.IsPersistent)
    {
(+)    DateTimeOffset expiresUtc = context.Properties.ExpiresUtc ?? issuedUtc.Add(Options.ExpireTimeSpan);
       cookieOptions.Expires = expiresUtc.ToUniversalTime().DateTime;
    } 
+2

Source: https://habr.com/ru/post/1536008/


All Articles