【问题标题】:Intranet ASP.NET MVC application with longer session具有较长会话的 Intranet ASP.NET MVC 应用程序
【发布时间】:2016-03-27 05:34:48
【问题描述】:

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

Web.Config 中的会话设置:

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

  1. 上述方法好吗?有更好的解决方案吗?
  2. 我可以让会话保持 8 小时吗?
  3. 在 IIS 服务器(7.5 版)上会出现任何负载问题吗?

编辑: 我还在预定时间(每天晚上 7 点)回收 AppPool。

【问题讨论】:

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


    【解决方案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/应用程序重启等之后将是活动的。


    【讨论】:

    • 我每天晚上 7 点回收 AppPool 并使用 Windows 身份验证。
    • 查看我上面的更新并尝试在发布应用程序的 IIS 服务器上使用 Keep Alive 服务。
    • 谢谢,穆拉特。答案实际上并没有回答我的问题,但可能对其他人有用。
    猜你喜欢
    • 1970-01-01
    • 2017-01-24
    • 1970-01-01
    • 1970-01-01
    • 2011-03-10
    • 1970-01-01
    • 2012-02-14
    • 2011-11-12
    • 2014-02-25
    相关资源
    最近更新 更多