如何更改.Net Core 3.1 Identity Server 4中的持久性Cookie过期时间

问题描述 投票:0回答:1

我正在使用.Net Core 3.1和Identity Server 4,要更改持久性cookie的到期时间。为此,请使用Startup.cs中的以下代码

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

[具有“保持原样”复选框,与“ isPersistent”绑定。使用上面的代码,如果SignIn带有“ isPersistent = true”,则Cookie会在5分钟后过期,并且可以在浏览器Cookie中看到enter image description here

并且在“ isPersistent = false”情况下,浏览器中的cookie如下所示enter image description here

但是在“ isPersistent = false”的情况下,cookie也将在5分钟后过期,但不应过期。我通过刷新页面进行检查,它重定向到登录页面。

如果不使用该代码,则“ isPerstent = false”正常工作。我只想更改持久性Cookie的到期时间。请帮助

c# .net-core identityserver4 identity .net-core-3.1
1个回答
0
投票

这是我的解决方案。需要覆盖“ SignInWithClaimsAsync”,如下所示

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);
}
© www.soinside.com 2019 - 2024. All rights reserved.