c# - 如何在 .Net Core 3.1 Identity Server 4 中更改持久性 Cookie 过期时间
问题描述
我正在使用 .Net Core 3.1 和 Identity server 4,想要更改持久性 cookie 过期时间。为此,在 Startup.cs 中使用以下代码
services.ConfigureApplicationCookie(options =>
{
options.ExpireTimeSpan = TimeSpan.FromMinutes(5);
});
有与“isPersistent”绑定的“Stay Singed In”复选框。使用上述代码,如果 SignIn 使用“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);
}
推荐阅读
- laravel - 仅当验证链接粘贴在用户登录的选项卡中时,Fortify 电子邮件验证才有效
- python-3.x - 当函数通过在单独的线程中按下控制台键自行运行时中断
- wordpress - 将相同的样式应用于 Oxygen Builder 中的可编辑块
- python - ValueError:数据基数不明确:x 大小:10 y 大小:1 请提供具有相同第一维的数据
- python - InvalidArgumentError:断言失败:[标签必须是 <= n_classes - 1] [条件 x <= y 没有按元素保持:] [x (head/losses/Cast:0) = ]
- swift - 如何等到 UIView 转换结束
- c++ - C++ 中“低级”容器数据的原始指针、智能指针或 std::vector
- html - 如何制作一个包含给定单元格数量的动态 HTML 表格?
- javascript - 无法对您进行身份验证 - twitter api v1.1
- cordova - admob 实时广告未在 ios 上显示,但在 android 上显示(产品)