cookies - Asp.net Core 2.0 Identity.TwoFactorRememberMe 过期

标签 cookies asp.net-core asp.net-identity two-factor-authentication

我一直在寻找,但找不到更改 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 以进行信息隐藏 - 快速谷歌搜索将通过查看默认 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");
}); 

请注意,选项 2 将 不工作如果您还使用 Identity Server,它会在 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");
}); 

关于cookies - Asp.net Core 2.0 Identity.TwoFactorRememberMe 过期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50628262/

相关文章:

python - 如何使用 Selenium RC 保存和恢复所有 cookie?

Angular 12 Production Build 生成大量 Lazy Chunk 文件

c# - .Net Core 2.0 中的 Windows 和匿名身份验证

c# - 正确使用 Microsoft.AspNet.Identity 2.0

c# - ASP.NET 身份电子邮件确认无效 token

c# - 在 ASP.NET Identity 2.0 中获取当前用户 ID

google-app-engine - 使用 appspot.com 作为部分域 cookie

Magento Cookie 问题。为什么会这样?

java - tomcat服务器关闭时如何删除cookies?

docker - docker-compose 中的 ASPNETCORE_URLS 未覆盖 appsetting.production.json