ASP. NET Session Object Persistence Session instructions

Source: Internet
Author: User

ASP. NET provides Session objects, allowing programmers to identify, store, and process the context information of several requests from the same browser object to a specific network application on the server. Session corresponds to the same conversation between the browser and the server. When the browser first requests a page of the network application, the server triggers the Session_onStart event; the Session_onEnd event is triggered when the dialog times out or is disabled. The programmer can respond to these two events in the code to process tasks related to the same conversation, such as opening up and releasing resources to be used for this conversation.

In ASP. to use the Session object in the NET program, make sure that the EnableSessionState attribute in the @ page command of the page is True or Readonly, and in the web. the SessionState attribute is correctly set in the config file.
Session status persistence in ASP. NET is determined by the mode attribute marked by the <system. web> label in the web. config file. This attribute has four possible values: Off, Inproc, StateServer, and SQlServer.

If it is set to Off, the Session will be disabled..
Inproc is the default setting. This mode is similar to the previous ASP session state method, and the session state is saved in ASP. NET process, its advantages are obvious: performance. Data access within a process is naturally faster than that of a process. However, the Session state of this method depends on the ASP. NET process. When the IIS process crashes or is restarted normally, the State stored in the process will be lost.
To overcome the disadvantages of Inproc mode, ASP. NET provides two methods to maintain the session status outside the process.
ASP. NET first provides a Windows Service: ASPState. After the service is started, ASP.. NET applications can set the mode attribute to "SateServer" to use the status management method provided by the Windows service.
In addition. if the mode attribute is set to StateServer In the config file, you must also set the IP address and port number of the StateServer server. if StateServer is run on the machine where IIS is located, the IP address is 127.0.0.1, and the port number is usually 42424. the configuration is as follows:

Mode = "StateServer"
StateConnectionString = "tcpip = 127.0.0.1: 42424"
In this mode, session state storage does not depend on the failure or restart of the IIS process, and the session state is stored in the memory space of the StateServer process.

Another session Status mode is SQLServer mode. This mode saves the session Status in the SQLServer database. Before using this mode, you must have at least one SQLServer server and create the required tables and stored procedures in the server .. NETSDK provides two scripts to simplify this process: InstallSqlState. SQL and UnInstallSqlState. SQL. Files in these two countries are stored in the following path:
<% SYSTEMDRIVER %> \ Winnt \ Microsoft. NET \ Framework \ <% version %> \
To configure the sqlserverserver server, you can run the sqlserver.exe command line tool in the command line.
Osql-s [servername]-u [user]-p [password] <InstallSqlState. SQL

For example:
Osql-s (local)-uas-p ""-iInstallSqlState. SQL
After necessary database preparation, change the mode attribute of the sessionstate element in the web. config file to "sqlserver" and specify the SQL connection string. The details are as follows:

Mode = "SQLServer"
SqlConnectionString = "datasource = 127.0.0.1; userid = sa; password =; Trusted_Connection = yes"
In SQL server mode, the Session state does not depend on the IIS server, and the SQL server cluster can be used to make the status storage independent from a single SQL Server, in this way, the application can be highly reliable.

Summary: Seesion maintains the session in two ways, and the cookie/is stored in the data.

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.