Session details in ASP. NET

Source: Internet
Author: User

In ASP. NETProgramTo use the session object, make sure that the enablesessionstate attribute in the @ page command of the page is true or readonly, And the sessionstate attribute is correctly set in the web. 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.
For example:

   <! --  <Sessionstate mode = "inproc" stateconnectionstring = "TCPIP = 127.0.0.1: 42424" sqlconnectionstring = "Data Source = 127.0.0.1; user id = sa; password = qazqaz "cookieless =" false "timeout =" 2 "/>  -->  
<! -- Note that the timeout value must be greater than 2 minutes. We recommend that you set the timeout value to 60 minutes, that is, 1 hour. This is not easy because of short-term Session Timeout. -->
< Sessionstate Mode = "StateServer" Stateconnectionstring = "TCPIP = FIG: 42424" Statenetworktimeout = "1000" Customprovider = "" Cookieless = "Usecookies" Cookiename = "ASP. net_sessionid" Timeout = "60" Allowcustomsqldatabase = "False" Regenerateexpiredsessionid = "True" Partitionresolvertype = "" Usehostingidentity = "True" />
<! -- <Sessionstate mode = "sqlserver" sqlconnectionstring = "Server = 192.168.69.27; database = sessionsave; uid = sa; Pwd = gpsdbsa; "allowcustomsqldatabase =" true "cookieless =" false "timeout =" 2000 "/> -->

 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 SQL Server database. Before using this mode, you must have at least one SQL Server server and create the required tables and stored procedures in the server .. . Net SDK provides two scripts to simplify this work: installsqlstate. SQL and uninstallsqlstate. SQL. These two files are stored in the following path:

 <% Systemdriver %> \ winnt \ Microsoft. NET \ framework \ <% version %> \

To configure the SQL Server server, you can run the SQL server command line tool osql.exe in the command line.

 Osql-s [server name]-U [user]-P [Password] <installsqlstate. SQL

For example:

 Osql-s (local)-u As-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 = "Data Source = 127.0.0.1; userid = sa; Password =; trusted_connection = yes"

In sqlserver mode, the session state is independent from 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.

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.