HttpApplication and Session HttpSessionState

Source: Internet
Author: User
Tags http cookie

Asp tutorial. net httpapplication and session (https tutorial essionstate)


The application object is shared in the application. All online ELE. Me users can modify the application object. It disappears only after the application ends. the session object is mainly for the user's individual objects. When the user goes online, the server creates a session for the user. When the user goes offline, the session object will be released. the lock of the application object is mainly used to lock access to the application state to promote access synchronization. take buying a train ticket for example. When a conductor sells a ticket, the ticket must be locked. If it is not locked, the same conductor may also sell the ticket at this time, lock is used to prevent this phenomenon.

 

Provides access to the session status value, session level settings, and TTL management methods.

Type: system. web. sessionstate. httpsessionstate
The asp.net tutorial provides session Status management, allowing you to store information associated with a unique browser session based on multiple requests. You can store a set of values referenced by key names or numeric indexes. You can use the httpsessionstate class to access session values and functions. This class can be accessed through the session attribute of the current httpcontext or the session attribute of the page.
Session data is associated with a specific browser session through a unique identifier. By default, this identifier is stored in the browser's non-expired session cookie, but you can also set the cookieless feature to true or useuri in the sessionstate element configured by the application, in this way, configure the application and store the session identifier in the url. You can specify the usedeviceprofile value of the cookieless feature so that asp.net can determine whether the browser supports cookies. You can also specify the autodetect value for the cookieless feature so that asp.net determines whether to enable cookie for the browser. If the browser supports cookies after usedeviceprofile is specified, or the browser enables cookies after autodetect is specified, the session identifier is stored in the cookie; otherwise, the session identifier is stored in the url.
During the first request, the session will start. If the browser does not send a new request before the timeout value reaches the specified minute, the current session value will remain valid. After a new session starts, the session start event is triggered. When a session starts, you can use this event to perform any other work, such as setting the default session value. If the session times out, the abandon method will be called or the asp.net application will be closed, which will cause the session end event. You can use this event to perform any necessary cleanup operations. The end event is triggered only when the session state mode is set to inproc.
To improve performance, session storage is only allocated for sessions that use cookies after the data is actually stored in the session object.
Session state persistence does not span the boundaries of asp.net applications. If the browser locates another application, the current session information is no longer valid for the new application.
By default, session values are stored in the memory of the web server. You can also store session values in SQL server database tutorials, asp.net status servers, or custom servers. Once the asp.net or iis process or asp.net application is restarted, this action can save the session value and make the session value available between all servers in the network farm. To configure this behavior, set the mode attribute to a valid sessionstatemode value in the sessionstate element configured in the application.
The session status may expire (by default, it expires 20 minutes after being inactive), and the stored information may be lost. You can use the timeout attribute of the sessionstate configuration section to control the session State Lifetime.
Common methods and attributes:
Common Methods:

// Add a new item to the session Status set.
Session. add (usernamezz );

// Delete the items in the session Status set. Return null When retrieving again
Session. remove (username );
Session. removeat (0 );

// Remove all keys and values from the session Status set.
Session. clear ();
Session. removeall ();

// Cancel the current session and start a new session, that is, a new session object
// Note: generally called when the session times out, you can set whether the new session uses the old sessionid
Session. abandon (); method:
// Define an array with a length. The value does not matter.
Object [] objs = new object [10];

// Copy the set of session Status values to the array. Starting from the specified index of the array, the value before the index remains unchanged, and the value after the index is overwritten.
// If the length of the array is not enough, an array out-of-bounds exception will be thrown...
Session. copyto (objs, 5 );

// How to fill in, how to obtain, should return value theoretically, but I debug multiple times, the return is a key, do not use this method.
Object obj5 = objs [5]; common attributes:
// Obtain or set the session value by digital index. Item [int32] (read/write attribute)
Session [0] = zz;
Object obj1 = session [0];

// Obtain or set the session value by name. Item [string] (read/write attribute)
Session [username] = zz;
Object obj2 = session [username];

// Obtain the number of items in the session state set.
Int count = session. count;

// Obtain the set of keys that store all values in the session state set.
// Using system. collections. specialized;
Nameobjectcollectionbase. keyscollection keys = session. keys;

// Obtain the unique identifier of a session.
String id = session. sessionid;

// Obtain a value indicating whether the session is created with the current request.
Bool isnewsession = session. isnewsession;

// Obtain a value indicating whether the session is read-only.
Bool isreadonly = session. isreadonly;

// Obtain a value indicating whether the session id is embedded in a url or stored in an http cookie.
Bool iscookieless = session. iscookieless;

// Obtain a value indicating whether the application is configured for a non-cookie Session. The default value is usecookies. (Read-only)
Httpcookiemode hm = session. cookiemode;

// Obtain the current session Status mode. The default value is inproc (read-only)
Sessionstatemode ssm = session. mode;

// Obtain and set the time allowed between requests before the session State provider terminates the session (in minutes, the default value is 20 ).
Int timeout = session. timeout;

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.