c# - 传递给 HttpContext.SignInAsync 和 CookieAuthenticationOptions.ExpireTimeSpan 的时间之间的差异
问题描述
传递给Microsoft.AspNetCore.Authentication.AuthenticationProperties.ExpiresUtc
inHttpContext.SignInAsync
和的日期 和有什么不一样Microsoft.AspNetCore.Authentication.Cookies.CookieAuthenticationOptions.ExpireTimeSpan
?
两者的文档都说它控制time at which the authentication ticket expires
. 那么一段时间后我应该使用哪个来放弃登录?
解决方案
基本上,是未设置时使用CookieAuthenticationOptions.ExpireTimeSpan
的默认值AuthenticationProperties.ExpiresUtc
。
登录时CookieAuthenticationHandler中的逻辑是这样的:
if (!signInContext.Properties.ExpiresUtc.HasValue)
{
signInContext.Properties.ExpiresUtc = issuedUtc.Add(Options.ExpireTimeSpan);
}
ExpiresUtc
当身份验证属性中有明确的设置时,就会使用它。否则,到期日期计算为发布时间加上ExpireTimeSpan
已配置的时间。
因此,您应该将 配置CookieAuthenticationOptions
为具有默认值,然后AuthenticationProperties
在您想要覆盖它时通过显式传递。例如,您需要通过AuthenticationProperties
设置持久性 cookie,但除非您有一些其他逻辑来计算到期日期,否则仅保留选项中的默认持续时间可能更容易。
推荐阅读
- javascript - 'Cron' 包在 nodeJS 中没有按预期工作
- c# - 在 Twilio 中使用 AMD 修改调用
- javascript - 如何在选择事件上通过更改 URL 重新加载 Jquery jtable
- javascript - var 未在角度测试中定义
- typescript - 如何在 Protractor-Jasmine-typescript html 报告日志中添加 Pass 语句
- mysql - 为什么使用 mysql 和 docker-compose 会发生访问被拒绝
- windows - 电子应用程序未显示 Windows 通知
- angular2-forms - Angular2如何处理.find方法中的空值
- java - 添加 Spring Security 会导致列和表丢失
- sql - Oracle SQL 比较聚合行