如何在 ASP.NET Core 中设置 cookie validateInterval?
Posted
技术标签:
【中文标题】如何在 ASP.NET Core 中设置 cookie validateInterval?【英文标题】:How to set the cookie validateInterval in ASP.NET Core? 【发布时间】:2016-09-16 02:43:37 【问题描述】:我正在尝试为使用 ASP.NET Identity 3
的 ASP.NET 5 RC1 应用程序设置 validateInterval
我正在尝试实现this答案中的代码。
有很多像this answer 这样的代码示例,但它似乎在 ASP.NET 5 RC1 中无效
app.UseCookieAuthentication(new CookieAuthenticationOptions
Provider = new CookieAuthenticationProvider
OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
validateInterval: TimeSpan.FromMinutes(15))
,
ExpireTimeSpan = TimeSpan.FromMinutes(30)
);
如果我尝试在ASP.NET 5 RC1
中使用上面的代码示例,我就不行了
Provider
不是CookieAuthenticationOptions
的属性
并且 Visual Studio 无法通过其灯泡选项在任何命名空间中找到 CookieAuthenticationProvider
。
如何在ASP.NET 5 RC1
中设置validateInterval
?
【问题讨论】:
【参考方案1】:验证间隔在 IdentityOptions 中设置:
services.AddIdentity<AppUser, AppRole>(options =>
options.SecurityStampValidationInterval = TimeSpan.FromMinutes(15);
您可以使用 CookieAuthenticationEvents 附加到验证事件:
app.UseCookieAuthentication(new CookieAuthenticationOptions()
Events = new CookieAuthenticationEvents()
OnValidatePrincipal = context =>
Microsoft.AspNet.Identity.SecurityStampValidator.ValidatePrincipalAsync(context);
return Task.FromResult(0);
,
,
ExpireTimeSpan = TimeSpan.FromMinutes(30)
);
【讨论】:
或者干脆return SecurityStampValidator.ValidatePrincipalAsync(context);
【参考方案2】:
从 ASP.NET Core 2.0 开始,当您 AddIdentity
时,您将无法设置 SecurityStampValidationInterval
。
您将能够通过SecurityStampValidatorOptions
设置ValidationInterval
:
services.Configure<SecurityStampValidatorOptions>(options =>
options.ValidationInterval = TimeSpan.FromSeconds(10);
);
P.S:你必须先AddIdentity
,然后再ConfigureApplicationCookie
。
【讨论】:
以上是关于如何在 ASP.NET Core 中设置 cookie validateInterval?的主要内容,如果未能解决你的问题,请参考以下文章
如何在 ASP.NET Core 2.0 中设置多个身份验证方案?
如何在 asp.net core 3 中设置 json 序列化程序设置?
如何在 asp.net core 中设置 jwt 令牌的生命周期
如何在 Asp.Net Core 2.0“AddJwtBearer”中间件中设置多个受众?