use*_*481 5 cookies two-factor-authentication asp.net-identity asp.net-core
我一直在寻找,但是当我调用signInManager.TwoFactorSignInAsync方法并将"remember client"参数设置为true时,我找不到改变Identity.TwoFactorRememberMe cookie的到期日期的方法.
此方法效果很好,但默认值为14天,但遗憾的是不适合客户.他们希望cookie更持久,因此他们的客户不会频繁填写2FA.
我正在使用asp .net核心2.1 - 到目前为止我遇到的任何答案都是旧版本的身份.
谢谢
小智 11
要为双因素 cookie 设置自定义过期时间,有两种不同的方法:
选项 1:在调用后将以下内容放入启动中services.AddAuthentication():
services.Configure<CookieAuthenticationOptions>
(IdentityConstants.TwoFactorRememberMeScheme, options =>
{
//this will override the default 14 day expire time
options.ExpireTimeSpan = TimeSpan.FromDays(30);
});
Run Code Online (Sandbox Code Playgroud)
尽管您还应该考虑重命名 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);
});
Run Code Online (Sandbox Code Playgroud)
选项 2:如果您将 AddIdentityCookies() 调用与 AddAuthentication() 调用结合使用,则可以更改名称和过期时间:
services.AddAuthentication().AddIdentityCookies(o =>
{
o.TwoFactorRememberMeCookie.Configure(a => a.Cookie.Name = "app.2fa.rememberme");
});
Run Code Online (Sandbox Code Playgroud)
请注意,如果您还使用 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");
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
407 次 |
| 最近记录: |