Session status in ASP. NET

Source: Internet
Author: User

Unlike cookies, Session status has no size limit. If there are extreme requirements, you can use Session to save G data. In addition, the Session can save more complex objects. For example, you can save a DataSet in the Session.

When a Session status is used, a cookie named ASP. NET_SessionId is automatically added to the browser. This cookie contains a unique identifier that can be used to track users during page switching.

By default, if the cookie is disabled, the Session status does not work properly. By default, if the user does not request any page for more than 20 minutes, the data stored in the Session state will be discarded.

ASP. NET Session Object

The main application programming interface used by users to operate the Session status is the HttpSessionState class. This object is exposed by Page. Session, Context. Session, UserControl. Session, WebService. Session, and Application. Session attributes. That is to say, you can access the Session status from anywhere.

HttpSessionState supports the following attributes:


CookieMode -- used to specify whether to enable cookie-independent Session status.
Count -- used to obtain the number of items in the Session state.
IsCookieless -- used to specify whether to enable the cookie-independent Session Status function.
IsNewSession -- used to check whether a new user session is created for the current request;
IsReadOnly -- read-only or not;
Keys -- used to obtain the name list of items saved in the Session state;
Mode -- specifies the storage handler in the current Session state.
SessionID -- used to obtain a unique session identifier;
Timeout -- used to specify the number of minutes for the Web server to assume that the user leaves and cancels the Session status. The maximum value is 525600 minutes and 1 year );
The HttpSessionState object also includes the following methods:
Abandon -- used to terminate a user session;
Clear -- used to Clear all items in the Session state;
Remove -- used to delete a specific project from the Session state;

ASP. NET submit session events

Global. asax contains two events that can be processed in the associated Session state: Session Start and Session End events. The Session Start event is triggered when a new Session starts, and the Session End event is triggered when the Session ends.

Session failure Control

By default, the user does not request a page within 20 minutes and the Session expires. You can modify the Session expiration time in the configuration file:

 
 
  1. <?xml version="1.0"?> 
  2. <configuration> 
  3. <system.web> 
  4. <sessionState timeout="60" /> 
  5. </system.web> 
  6. </configuration> 

You can also change the expiration time of the Session state by programming.
Session. Timeout = 60;

Session status using Cookieless

By default, Session status depends on cookie. If the cookie is disabled in the browser, the Session status cannot work by default. In this case, you can use a non-cookie Session. When a non-cookie Session is enabled, the user's session ID will be added to the URL of the page.

You can modify the sessionState element of the configuration file and set cookieless to AutoDetect.

 
 
  1. <?xml version="1.0"?> 
  2. <configuration> 
  3. <system.web> 
  4. <sessionState cookieless="AutoDetect" 
  5. regenerateExpiredSessionId="true" /> 
  6. </system.web> 
  7. </configuration> 
  1. SQL statements in ASP. NET
  2. Use StringBuilder in ASP. NET
  3. Response. Write in ASP. NET
  4. Analysis on ASP. NET authorization Module
  5. Processing Process for ASP. NET Runtime

Related Article

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.