Timeout 属性的值可以设置为 525,600 分钟(1 年)。默认值为 20 分钟。另一方面,请注意,如果您有大量用户,则会出现性能问题,因为会话超时会增加,您的非活动会话将保留在 Web 服务器内存中,这可能会导致应用程序池回收,从而导致丢失所有会话对于所有用户。
您可以在 web.config 中设置会话超时,如下所示:
web.config:
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" executionTimeout="1800" />
<sessionState mode="InProc" timeout="30" />
<!-- For LDAP -->
<httpCookies httpOnlyCookies="true" />
<authentication mode="Forms">
<!-- Note: I also remove this part and try with only "sessionState" -->
<forms name=".ADAuthCookie" loginUrl="~/Account/Login" timeout="1"
slidingExpiration="false" protection="All" />
</authentication>
</system.web>
另一方面,如果您使用ASP.NET Identity,则无需使用web.config 中的设置。只需将这两行添加到您的 UseCookieAuthentication() 方法中,如下所示:
....,
SlidingExpiration = true,
ExpireTimeSpan = TimeSpan.FromMinutes(1)
...
所以,你的方法的最终代码如下所示:
Startup.Auth.cs:
public void ConfigureAuth(IAppBuilder app)
{
// Code removed for brevity.
// Configure the sign in cookie
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login"),
Provider = new CookieAuthenticationProvider
{
// Enables the application to validate the security stamp when the user logs in.
// This is a security feature which is used when you change a password or add an external login to your account.
OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
validateInterval: TimeSpan.FromMinutes(30),
regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
},
SlidingExpiration = true,
ExpireTimeSpan = TimeSpan.FromMinutes(30) //Set the session timeout at here
});
app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
app.UseTwoFactorSignInCookie(DefaultAuthenticationTypes.TwoFactorCookie, TimeSpan.FromMinutes(1));
app.UseTwoFactorRememberBrowserCookie(DefaultAuthenticationTypes.TwoFactorRememberBrowserCookie);
}
希望这会有所帮助...
更新:
关于第二个问题,问题与IIS有关,而不是与调度程序Quartz.NET、Hangfire等有关。另一方面,网上有很多解决方法,但只有一些其中一个正在工作。在我看来,没有必要应用大量的配置设置。只需在您发布应用程序的服务器上安装Keep Alive Service For IIS 6.0/7.5 即可享受。那么你发布的应用程序在应用程序池回收、IIS/应用程序重启等之后将是活动的。