asp.net web.config 設定Session到期時間

來源:互聯網
上載者:User

標籤:

在Asp.net中,可以有四處設定Session的到期時間:(原文作者:望月狼地址:http://www.cnblogs.com/wangyuelang0526/)

一、全域網站(即伺服器)級

IIS-網站-屬性-Asp.net-編輯配置-狀態管理-會話逾時(分鐘)-設定為120,即為2小時,即120分鐘後如果目前使用者沒有操作,那麼Session就會自動到期。

二、網站級

IIS-網站-具體網站(如DemoSite)-屬性-Asp.net,此時有兩個選項,一個是“編輯全域配置”,一個是“編輯配置”。

如果“編輯全域配置”,就和上個配置一樣。

如果“編輯配置”,則只對當前網站生效。因為一個伺服器可能有很多獨立網站。

1、繼續選擇“狀態管理”-會話逾時(分鐘)-設定為360,即360分鐘。效果同上,只不過只對當前網站生效。

2、身份認證-Forms-Cooke逾時,選擇"12:00:00",即12個小時。可選項共有以下八項:

00:15:00

00:30:00

01:00:00

02:00:00

04:00:00

08:00:00

12:00:00

1:00:00:00

即最長24小時,最小15分鐘。這是預設的配置。在應用中可以自由定製。

三、應用程式級

同網站管理,只不過範圍僅限當前應用程式。

四、頁面級

在某頁面中,設定Session.Timeout = 30;即可臨時修改某頁面的會話到期時間。

查看某個Session的到期時間,可以用

view plaincopy to clipboardprint? TimeSpan SessTimeOut = new TimeSpan(0, 0, System.Web.HttpContext.Current.Session.Timeout, 0, 0);  TimeSpan SessTimeOut = new TimeSpan(0, 0, System.Web.HttpContext.Current.Session.Timeout, 0, 0);

其中,二和三的設定,體現在Web.config中即:

<?xml version="1.0"?>  <configuration>  <system.web>  <authentication mode="Forms" > <forms name="AuthLogin" loginUrl="/Login.aspx" protection="All" timeout="360" slidingExpiration="true"/> </authentication> <sessionState mode="InProc" cookieless="false" timeout="20" /> </system.web> <location path="Login.aspx"> <system.web> <authorization> <allow users="*" /> </authorization> </system.web> </location> </configuration> <?xml version="1.0"?><configuration><system.web><authentication mode="Forms" ><forms name="AuthLogin" loginUrl="/Login.aspx" protection="All" timeout="360" slidingExpiration="true"/></authentication><sessionState mode="InProc" cookieless="false" timeout="20" /></system.web><location path="Login.aspx"><system.web><authorization><allow users="*" /></authorization></system.web></location></configuration> 

以上四處設定的優先順序為頁面級>應用程式級>網站級>伺服器級。換句話說,如果版面設定為20分鐘,網站設定為120分鐘,那麼,顯然以20分鐘為生效的到期時間。

另外一個值得注意 的地方。

在設定二處,設定會話逾時(SessionState)120分鐘,而同時用forms認證,設定為“00:15:00”,即15分鐘,並且slidingExpirationo為false,則真正生效的Session到期時間是多少呢?

有效結果是SessionState的設定,即120分鐘。

如果有設定Session到期時間沒有生效的,請檢查以上幾處配置。

asp.net web.config 設定Session到期時間

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.