Summary on setting session expiration time in asp

Source: Internet
Author: User

If the session expiration time is not set in the program, the session expiration time is executed according to the expiration time set by IIS. The default expiration time of the session in IIS is 20 minutes, the session time in IIS can be changed.

Set the time before

For exampleCopy codeThe Code is as follows: Session. Timeout = 30' SEESION effective time is 30 minutes
Session ("ID") = Rs ("id ")
Session ("Name") = Rs ("Name ")
Session ("Pass") = Rs ("Pass"

Use the Session. Timeout attribute to set the Timeout period

For a user logging on to an ASP application, if the user does not perform any other operations within the system's default time, the user's Session will be automatically revoked as soon as the set time arrives, this prevents system resources from being wasted. The TimeOut attribute of the Session object can be used to set the "expiration time" in minutes. The format is as follows:

Copy codeThe Code is as follows: Session. TimeOut = MaxTime

Instance Code: (5.asp) page, this instance describes how to control the end of a session.Copy codeThe Code is as follows: <% @ language = "vbscript" %>
<% Session. timeout = 60%>
<Html>
<Head> <title> control the end time of a session </title>
<Body>
<%
Who = Session. SessionID
CurrentPage = Request. ServerVariables ("SCRIPT_NAME ")
Response. AppendTolog who & ":" & CurrentPage
Response. write "<center> your session ID is:" & who & "<p>"
Response. write "the path to your current page is:" & CurrentPage & "<p>"
If Session ("I") = "" then
Session ("I") = 1
Else
Session ("I") = session ("I") + 1
End if
Session. Abandon
Response. write "you refreshed this page" & Session ("I") & "times. </Centr>"
%>

In Asp.net applications, many people may encounter conflicting Session expiration settings. Here, you can set the expiration time of the Session in four ways:

1. Global websites (servers)

IIS-website-properties-Asp.net-edit configuration-state management-Session Timeout (minutes)-set to 120, that is, 2 hours, that is, if no operation is performed by the current user after 120 minutes, the Session will automatically expire.

2. website-level

IIS-website-specific website (such as DemoSite)-attribute -Asp.net, there are two options: "Edit global configuration" and "Edit configuration ".
If you click "Edit global configuration", it is the same as the previous configuration.
If you select "Edit configuration", it only takes effect for the current website. Because a server may have many independent websites.
1. Choose "status management"> "Session Timeout (minutes)"> "360", that is, 360 minutes. The effect is the same as that of the current website.
2. Authentication-Forms-coke times out. Select "12:00:00", that is, 12 hours. There are eight optional items:
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
That is, the maximum is 24 hours, and the minimum is 15 minutes. This is the default configuration. You can customize your applications.

3. Application-level

Same as website management, but the scope is limited to the current application.

4. Page-level

On a page, set Session. Timeout = 30; to temporarily modify the Session expiration time of a page.
View the expiration time of a Session. You can useCopy codeThe Code is as follows: 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 );

The second and third settings are reflected in Web. config:Copy codeThe Code is as follows: 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>

The priority set above is page-level> application-level> website-level> server-level. In other words, if the page is set to 20 minutes and the website is set to 120 minutes, then the expiration time is obviously 20 minutes.

Another area worth noting.

In step 2, set the Session Timeout (SessionState) to 120 minutes, and use forms authentication at the same time, set it to "00:15:00", that is, 15 minutes, And slidingExpirationo is false, what is the expiration time of the Session that actually takes effect?

The valid result is the SessionState setting, that is, 120 minutes.

If the Session expiration time has not been set to take effect, check the above configurations.

Other methods found online

1. OS: Widnows Server 2003
Step: start --> Administrative Tools --> Internet Information Service (IIS) manager --> website --> default website --> right-click "property" --> main directory --> Configuration --> options --> enable session status --> Session Timeout (set the timeout here) time, in minutes ). OK.
2. Set the Session expiration time in ASP. NET Applications
In Web applications such as ASP. NET, Session is a common method to save user statuses. However, due to limited server memory space, it is necessary to set the Session expiration time. In ASP. NET, how does one set the Session expiration time? It's easy to modify the web. config configuration.
The specific modification method is as follows:Copy codeThe Code is as follows: <system. web>
<SessionState mode = "InProc" timeout = "30"/>
</System. web>

This indicates that the Session expiration time is 30 minutes. That is to say, if the current user does not operate 30 minutes later, the Session will automatically expire.
3. On the cs page of the Session call, write the following in the Load event:Copy codeThe Code is as follows: Session. Timeout = 30;

4. Store session in asp.net state serviceCopy codeThe Code is as follows: <sessionState cookieless = "false" timeout = "480" mode = "StateServer" stateConnectionString = "tcpip = 127.0.0.1: 42424" sqlConnectionString = "data source = 127.0.0.1; user id = sa; password = "/>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.