如何在 ASP.NET Core MVC 中设置会话超时/到期时间? [英] How to set session timeout / expiry time in ASP.NET Core MVC?
问题描述
我正在使用 ASP.NET 零版本 7 的 ASP.NET Core、MVC 和 jQuery 项目.
I am using ASP.NET Zero version 7 of ASP.NET Core, MVC and jQuery project.
我正在尝试设置会话超时/到期时间,以便在应用程序空闲一段时间后自动从应用程序注销.任何人都可以请告诉我如何做到这一点?
I am trying to set session timeout / expiry time to automatically log out from the application when the application is idle for some time. Can anybody please let me know how to do this?
在 ASP.NET 零版本 8 中,他们在用户管理设置中提供此配置.
In ASP.NET Zero version 8, they are providing this configuration at User Management settings.
推荐答案
ASP.NET Core MVC
MVC 的会话过期由 ASP.NET Core 通过 cookie 提供,独立于 ASP.NET 零.
ASP.NET Core MVC
Session expiry for MVC is provided via cookie by ASP.NET Core, independent of ASP.NET Zero.
在Startup.cs中的IdentityRegistrar.Register
之后调用ConfigureApplicationCookie
:
public IServiceProvider ConfigureServices(IServiceCollection services)
{
// ...
IdentityRegistrar.Register(services); // No change
AuthConfigurer.Configure(services, _appConfiguration); // No change
services.ConfigureApplicationCookie(o =>
{
o.ExpireTimeSpan = TimeSpan.FromHours(1);
o.SlidingExpiration = true;
});
// ...
}
来自 ASP.NET Core v2.2.8 CookieAuthenticationOptions.cs#L30-L36:
public CookieAuthenticationOptions()
{
ExpireTimeSpan = TimeSpan.FromDays(14);
ReturnUrlParameter = CookieAuthenticationDefaults.ReturnUrlParameter;
SlidingExpiration = true;
Events = new CookieAuthenticationEvents();
}
ASP.NET 零(用于 ASP.NET Core)
ASP.NET Zero v7.2.0+ 提供:
ASP.NET Zero (for ASP.NET Core)
ASP.NET Zero v7.2.0+ provides:
- 对于MVC:前端配置和倒计时模式.
文档:aspnet-core-mvc/v7.2.0/Features-Mvc-Core-Tenant-Settings#user-management - 对于 Angular:通过令牌、前端配置和倒计时模式实现会话超时.
文档:aspnet-core-angular/v7.2.0/Features-Angular-Tenant-Settings#user-management
这篇关于如何在 ASP.NET Core MVC 中设置会话超时/到期时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!