且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在ASP.NET Core MVC中设置会话超时/到期时间?

更新时间:2022-05-26 01:19:44

ASP.NET Core MVC

MVC的会话过期由ASP.NET Core通过cookie提供,独立于ASP.NET Zero.

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 CookieAuthenticationAuthentications.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: