Asp.NET設置Session過期時間的四種方式


在Asp.net中,可以有四處設置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中即:

 
<?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過期時間沒有生效的,請檢查以上幾處配置。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM