如何在 .Net Core 3.1 Identity Server 4 中更改持久性 Cookie 过期时间
How to Change Persistent Cookie Expiration Time in .Net Core 3.1 Identity Server 4
我正在使用 .Net Core 3.1 和身份服务器 4,想更改持久性 cookie 过期时间。为此,请使用 Startup.cs
中的以下代码
services.ConfigureApplicationCookie(options =>
{
options.ExpireTimeSpan = TimeSpan.FromMinutes(5);
});
有 "Stay Singed In" 复选框与 "isPersistent" 绑定。
使用上面的代码,如果使用 "isPersistent=true" 登录,则 Cookie 将在 5 分钟后过期,并且可以在浏览器 cookie 中看到
如果 "isPersistent=false" 浏览器中的 cookie 如下所示
但在 "isPersistent=false" 的情况下,cookie 也会在 5 分钟后过期,这不应该。我通过刷新页面检查,它重定向到登录页面。
如果不使用该代码,则 "isPerstent=false" 工作正常。我只想更改 Persistent Cookie 的过期时间。请帮忙
这是我的解决方案。需要像下面那样覆盖 "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);
}
我正在使用 .Net Core 3.1 和身份服务器 4,想更改持久性 cookie 过期时间。为此,请使用 Startup.cs
中的以下代码services.ConfigureApplicationCookie(options =>
{
options.ExpireTimeSpan = TimeSpan.FromMinutes(5);
});
有 "Stay Singed In" 复选框与 "isPersistent" 绑定。
使用上面的代码,如果使用 "isPersistent=true" 登录,则 Cookie 将在 5 分钟后过期,并且可以在浏览器 cookie 中看到
如果 "isPersistent=false" 浏览器中的 cookie 如下所示
但在 "isPersistent=false" 的情况下,cookie 也会在 5 分钟后过期,这不应该。我通过刷新页面检查,它重定向到登录页面。
如果不使用该代码,则 "isPerstent=false" 工作正常。我只想更改 Persistent Cookie 的过期时间。请帮忙
这是我的解决方案。需要像下面那样覆盖 "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);
}