2014-02-24 39 views
13

我嘗試下面的代碼,以增加會話超時,但沒有用,如何增加asp.net中的會話超時?

代碼:

<sessionState mode="InProc" cookieless="true" timeout="60"> 
</sessionState> 

而且代碼在

void Session_Start(object sender, EventArgs e) 
{ 
// Code that runs when a new session is started 
Session.Timeout = 15; 
} 
+0

這應該工作。你如何檢查你的會話是否超時? – Paddy

+1

在這個問題採取掠奪http://stackoverflow.com/questions/8656463/increasing-session-timeout – Ehsan

回答

20

可以增加在ASP中的會話超時。以下列任何一種方式淨化:

使用IIS版本7:

  1. 打開了IIS
  2. 從網站列表中選擇您的網站
  3. 點擊上
  4. 現在下的cookie設置輸入您的會話超時權會話狀態

Web.config文件: 打開你的web.config文件,下的System.Web部分添加以下內容:

<sessionState timeout = "20" mode = "InProc" /> 

更換20與您所希望的任何號碼。

OR

Global.asax文件: 根據在session_start方法,在會議超時屬性設置爲需要的值,這樣

Session.Timeout = "20"; 

注:如果您在IIS和web.config中都設置會話超時,那麼IIS中的會覆蓋web.config中的那個

希望這有助於!

+1

你忘記關閉在你的例子sessionState標記。你應該寫: miguelbgouveia

+0

嘿謝謝你通知我,會用它更新我的答案!非常感激! – Neville

+2

實際上,在IIS 7下,將會話設置爲一個地方將其設置在另一個地方。所以如果你在IIS中設置它,它會自動更新Web.config,反之亦然。 – d512

0

如果您使用表單身份驗證,則會話超時的默認值爲30分鐘。請嘗試此代碼以增加會話超時。

<system.web> 
    <authentication mode="Forms"> 
     <forms timeout="70"/> 
    </authentication> 
    <sessionstate timeout="80"/> 
</system.web> 

我認爲代碼可以幫助你。

+0

嗨,你能告訴我兩個超時值之間的區別嗎?即'形式'超時&'Sessionstate'超時 –

+0

@NitinSawant - 這是一個現有的答案在您的查詢的計算器stackoverflow --- http://stackoverflow.com/questions/17812994/forms-authentication-timeout-vs-sessionstate-timeout – Neville

-1
<system.web> 
    <authentication mode="Forms"> 
     <forms timeout="70"/> 
     </authentication> 
      <sessionState timeout="80"/> 
       </system.web> 

它爲我工作

把它複製到你的webconfig文件

0

我想補充我的最終解決方案。讀取設置後,它在配置中是「不正確的」。

  if (model.RememberMe) 
      { 
       var ASPCookie = Request.Cookies["ASP.NET_SessionId"]; 
       ASPCookie.Expires = DateTime.Now.AddDays(15); 
       Response.SetCookie(ASPCookie); 
      } 
0

簡單,去WebConfig,然後將其設置,

 <system.web> 
    <sessionState timeout="60"></sessionState> 
    <compilation debug="true" targetFramework="4.0" /> 
</system.web>