Intranet ASP.NET MVC应用程序具有更长的会话

时间:2016-03-25 19:58:19

标签: asp.net-mvc session session-state

我正在使用 Windows身份验证为近2000名用户(以及 ~1200位并发用户)开发Intranet ASP.NET MVC 4.5应用程序。要求是让会话保持活动8小时。我只在 Session_Start (在Global.asax.cs中)中存储会话对象ID和名称中的两个变量。 jQuery会话中存储的变量很少(不在服务器上)

Web.Config中的会话设置:

如果有会话超时,那么我将用户重定向到主页(创建 BaseController 并覆盖 OnActionExecuting 功能)。

  1. 上述方法好吗?更好的解决方案?
  2. 我可以将会话保持8小时吗?
  3. IIS服务器(版本7.5)上是否会出现任何负载问题?
  4. 编辑: 我也在预定时间(每天晚上7点)回收AppPool。

1 个答案:

答案 0 :(得分:1)

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.NETHangfire等有关。另一方面,有很多解决方法发布在网络,但只有其中一些正在运作。在我看来,没有必要应用大量的配置设置。只需在您发布应用程序并享受的服务器上安装Keep Alive Service For IIS 6.0/7.5即可。然后,在应用程序池回收,IIS /应用程序重新启动等之后,您发布的应用程序将处于活动状态。