cookies - Asp.net Core 2.0 Identity.TwoFactorRememberMe 到期
问题描述
我一直在寻找,但找不到更改 Identity.TwoFactorRememberMe cookie 的到期日期的方法,该 cookie 在您调用 signInManager.TwoFactorSignInAsync 方法并将“记住客户端”参数设置为 true 时设置。
此方法效果很好,但默认为 14 天,不幸的是不适合客户。他们希望 cookie 更持久,这样他们的客户就不会经常填写 2FA。
我正在使用 asp .net core 2.1 - 到目前为止我遇到的任何答案都适用于旧版本的身份。
谢谢
解决方案
要为双因素 cookie 设置自定义过期时间,看起来有两种不同的方法:
选项 1:在通话后将以下内容放入您的启动中services.AddAuthentication()
:
services.Configure<CookieAuthenticationOptions>
(IdentityConstants.TwoFactorRememberMeScheme, options =>
{
//this will override the default 14 day expire time
options.ExpireTimeSpan = TimeSpan.FromDays(30);
});
尽管您还应该考虑重命名 cookie 以隐藏信息 - 快速 google 搜索会通过查看默认 cookie 名称来显示您正在使用 asp.net 身份。这可以通过 Cookie.Name 属性同时更改:
services.Configure<CookieAuthenticationOptions>
(IdentityConstants.TwoFactorRememberMeScheme, o =>
{
//this will override the default cookie name for information hiding
o.Cookie.Name = "app.2fa.rememberme";
//this will override the default 14 day expire time to 30 days
o.ExpireTimeSpan = TimeSpan.FromDays(30);
});
选项 2:如果您将 AddIdentityCookies() 调用与 AddAuthentication() 调用一起使用,则可以更改名称和过期时间:
services.AddAuthentication().AddIdentityCookies(o =>
{
o.TwoFactorRememberMeCookie.Configure(a => a.Cookie.Name = "app.2fa.rememberme");
});
请注意,如果您还使用 Identity Server,则选项 2 将不起作用,因为它在 UseIdentityServer() 调用期间调用它。
作为参考,我通过查看身份测试了解了如何执行此操作:https ://github.com/aspnet/Identity/blob/c7276ce2f76312ddd7fccad6e399da96b9f6fae1/test/Identity.Test/IdentityOptionsTest.cs#L77 。这在我能找到的任何地方都没有记录,我努力最终弄清楚了这一点。希望这有助于下一个来寻找如何做到这一点的人。
在信息隐藏的主题上 - 您可能还需要考虑重命名成功登录后代码验证期间使用的 TwoFactorUserId cookie。除了 IdentityConstant 稍有不同外,这可以用相同的方式完成:
services.Configure<CookieAuthenticationOptions>
(IdentityConstants.TwoFactorUserIdScheme, options =>
{
options.Cookie.Name = "app.2fa.userid";
});
services.AddAuthentication().AddIdentityCookies(o =>
{
o.TwoFactorUserIdCookie.Configure(a => a.Cookie.Name = "app.2fa.userid");
});
推荐阅读
- django - 当调试 = false 时,apache 不服务媒体文件 django
- python-3.x - 如何在 python 中一起使用多个 bigquery 项目
- css - WordPress网站页面和帖子(左右两侧)的填充调整
- excel - 需要很长时间的简单形式的循环公式
- python - 如何为每个新循环创建新的屏幕截图?
- javascript - 为什么登录后没有重新渲染?
- c# - 使用属性覆盖数据模型约定
- hyperledger-fabric - 为 Raft 生成 orderer genesis block 返回不明确的日志
- java - 来自非实体类的 JPA 继承属性
- angular - 角度指令:不同模块中的声明无法识别