且构网

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

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

更新时间:2021-10-18 02:15:59

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: