2016-03-25 28 views
-1

Intranet的ASP.NET MVC應用程序,我開發一個內部網ASP.NET MVC 4.5應用近2000用戶(〜1200個併發用戶)與Windows身份驗證。要求是讓會話存活8小時。我在Session_Start(在Global.asax.cs中)中僅存儲了會話對象ID和名稱中的兩個變量值。有存儲在jQuery的會話幾個變量(服務器不在)較長的會話

web.config中的會話設置:

如果有一個會話超時,然後我將用戶重定向到主頁(創建了一個基本控制器並覆蓋OnActionExecuting函數)。

  1. 上述方法是否好?有更好的解決方案
  2. 我可以保持會話8小時嗎?
  3. 在IIS服務器(版本7.5)上是否會有負載問題?

編輯: 我也在預定時間(每天下午7點)回收應用程序池。

回答

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 /應用重啓活着,等


+0

我每天回收的應用程序池晚上7點到使用Windows身份驗證。 – JSK

+0

請參閱上面的我的更新,並嘗試在發佈應用程序的IIS服務器上使用Keep Alive Service。 –

+0

謝謝,穆拉特。答案並沒有真正回答我的問題,但也許對別人有用。 – JSK