简体   繁体   中英

How to Change Persistent Cookie Expiration Time in .Net Core 3.1 Identity Server 4

I am using .Net Core 3.1 and Identity server 4, want to change persistent cookie expiration time. For this using below code in Startup.cs

services.ConfigureApplicationCookie(options =>
{
     options.ExpireTimeSpan = TimeSpan.FromMinutes(5);
});

There is "Stay Singed In" checkbox which bind with "isPersistent". With above code if SignIn with "isPersistent=true" then Cookie Expires in 5 min and can see this in browser cookie在此处输入图片说明

And in case of "isPersistent=false" cookie in browser looks like below在此处输入图片说明

but in case of "isPersistent=false" cookie also get expired after 5 min which should not. I checked by refresh the page, it redirect to login page.

If not using that code then "isPerstent=false" is working fine. I want to change only expiration time of Persistent Cookie. Please help

Here is my solution. Need to override "SignInWithClaimsAsync" like below

public override async Task SignInWithClaimsAsync(ApplicationUser user, AuthenticationProperties authenticationProperties, System.Collections.Generic.IEnumerable<System.Security.Claims.Claim> additionalClaims)
{
    if (authenticationProperties != null && authenticationProperties.IsPersistent)
    {
        authenticationProperties.ExpiresUtc = DateTimeOffset.UtcNow.AddMinutes(30); // for 30 mins
    }

    await base.SignInWithClaimsAsync(user, authenticationProperties, additionalClaims);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM