如何在 ASP.NET Core MVC 中设置会话超时/到期时间? [英] How to set session timeout / expiry time in ASP.NET Core MVC?

查看:142
本文介绍了如何在 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:

这篇关于如何在 ASP.NET Core MVC 中设置会话超时/到期时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆