Session過期時間問題
在Asp.net應用中,很多人會遇到Session過期設置有衝突。其中,可以有四處設置Session的過期時間:
一、全局網站(即服務器)級
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中即:
view plaincopy to clipboardprint?
<?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過期時間沒有生效的,請檢查以上幾處配置。
一、全局網站(即服務器)級
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中即:
view plaincopy to clipboardprint?
<?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過期時間沒有生效的,請檢查以上幾處配置。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.