Session configuration anatomy in web. config

Source: Internet
Author: User
After opening the configuration file Web. config of an application, we will find the following section:

<SessionState
Mode = "InProc"
StateConnectionString = "tcpip = 127.0.0.1: 42424"
SqlConnectionString = "data source = 127.0.0.1; Trusted_Connection = yes"
Cookieless = "false"
Timeout = "20"
/>


This section describes how the application stores Session information. The following operations mainly aim at this configuration section. Let's take a look at the meaning of the content contained in this section. The syntax of the sessionState node is as follows:

<SessionState mode = "Off | InProc | StateServer | SQLServer"
Cookieless = "true | false"
Timeout = "number of minutes"
StateConnectionString = "tcpip = server: port"
SqlConnectionString = "SQL connection string"
StateNetworkTimeout = "number of seconds"
/>

The required attribute is

Attribute option description
Mode setting: Where to store Session information
Off is set to not use the Session Function
InProc is set to store sessions in the process, which is the storage method in ASP. This is the default value.
StateServer is set to store sessions in independent State services.
SQLServer settings store sessions in SQL Server.

Optional attributes:

Attribute option description
Cookieless sets where the Session information of the client is stored
Ture uses Cookieless Mode
False uses Cookie mode, which is the default value.
Timeout specifies the number of minutes after which the server automatically waives the Session information. The default value is 20 minutes.
StateConnectionString is the name and port number of the server used to store Session information in the status service, for example, "tcpip = 127.0.0.1: 42424 ". This attribute is required when the mode value is StateServer.
SqlConnectionString sets the connection string when connecting to SQL Server. For example, "data source = localhost; Integrated Security = SSPI; Initial Catalog = northwind ". This attribute is required when the mode value is SQLServer.
StateNetworkTimeout sets the number of seconds after the Session state is stored in StateServer mode and the TCP/IP connection between the Web server and the server that stores the status information. The default value is 10 seconds.

Storage of client Session Status in ASP. NET
In our previous Session model introduction, we can find that the Session status should be stored in two places: client and server. The client is only responsible for saving the SessionID of the corresponding website, while other Session information is stored on the server. In ASP, the SessionID of the client is actually stored as a Cookie. If the user chooses to disable cookies in the browser settings, then he will not be able to enjoy the convenience of the Session, or even access some websites. To solve the above problems, the Session information storage methods of the client in ASP. NET are divided into Cookie and Cookieless.

In ASP. NET, by default, Session information is stored on the client using cookies. If you want to use Cookieless on the client to store Session information, the method is as follows:

Find the root directory of the current Web application, open the Web. Config file, and find the following section:

<SessionState
Mode = "InProc"
StateConnectionString = "tcpip = 127.0.0.1: 42424"
SqlConnectionString = "data source = 127.0.0.1; Trusted_Connection = yes"
Cookieless = "false"
Timeout = "20"
/>

In this section, cookieless = "false" is changed to cookieless = "true". In this way, the Session information of the client is no longer stored using cookies, but stored through URLs. Close the current IE, open a new IE, and re-access the Web application, you will see something similar to the following:

Http: // localhost/MyTestApplication/(ulqsek45heu3ic2a5zgdl245)/default. aspx indicates the Session ID of the client. Note that this information is automatically added by IIS and does not affect the normal connection.

Storage of server Session Status in ASP. NET
Preparations

To better experience the experiment, you can create a page named SessionState. aspx and add the following code to <body> </body>.

<Scriptrunat = "server">
Sub Session_Add (sender As Object, e As EventArgs)
Session ("MySession") = text1.Value
Span1.InnerHtml = "Session data updated! <P> Your session contains: <font color = red> "& Session (" MySession "). ToString () &" </font>"
End Sub

Sub CheckSession (sender As Object, eAs EventArgs)
If (Session ("MySession") Is Nothing) Then
Span1.InnerHtml = "NOTHING, session data lost! "
Else
Span1.InnerHtml = "Your session contains: <font color = red>" & Session ("MySession"). ToString () & "</font>"
End If
End Sub
</Script>
<Formrunat = "server" id = "Form2">
<Inputid = "text1" type = "text" runat = "server" name = "text1">
<Inputtype = "submit" runat = "server" OnServerClick = "Session_Add"
Value = "Add to Session State" id = "Submit1" name = "Submit1">
<Inputtype = "submit" runat = "server" OnServerClick = "CheckSession"
Value = "View Session State" id = "Submit2" name = "Submit2">
</Form>
<Hrsize = "1">
<Fontsize = "6"> <spanid = "span1" runat = "server"/> </font>

This SessionState. aspx page can be used to test whether Session information is lost on the current server.

Store Server Session information in the process
Let's go back to the section in the Web. config file:

<SessionState
Mode = "InProc"
StateConnectionString = "tcpip = 127.0.0.1: 42424"
SqlConnectionString = "data source = 127.0.0.1; Trusted_Connection = yes"
Cookieless = "false"
Timeout = "20"
/>

When the mode value is InProc, it indicates that the server is using this mode.

This method is the same as the previous ASP mode, that is, the server stores Session information in the IIS process. When IIS is disabled or restarted, the information is lost. However, this mode also has its own biggest advantage, that is, the highest performance. It should be that all Session information is stored in the IIS process, so IIS can quickly access this information, the performance of this mode is much faster than that of Session information stored outside the process or stored in SQL Server. This mode is also the default mode for ASP. NET.

Now let's do a test. Open the SessionState. aspx page and enter some characters to store them in the Session. Then, let's restart IIS. Note that it is not to stop the current site and start again, but to right-click the node of the machine name in IIS and choose restart IIS. (To restart IIS when NT4 is used, you must restart the computer. Microsoft returns SessionState. on the aspx page, check the Session information and find that the information has been lost.

Store Server Session information outside the process
First, let's open the management tool> Service, find the Service named ASP. NET State Service, and start it. In fact, this service is to start a process to save Session information. After starting this service, you can see a process named aspnet_state.exe in the Windows Task Manager> process. This is the process for saving Session information.

Return to the preceding section in the Web. config file and change the mode Value to StateServer. Open another IE after saving the file, open the SessionState. aspx page, and save some information to the Session. At this time, let's restart IIS and return to the SessionState. aspx page to view the Session information.

In fact, this method of storing Session information outside the process not only means that the information can be stored in the local process, but also the Session information can be stored in other server processes. In this case, you not only need to change the mode Value to StateServer, but also need to configure the corresponding parameters in stateConnectionString. For example, if your calculation is 192.168.0.1 and you want to store the Session in the process of a computer whose IP address is 192.168.0.2, you need to set it to stateConnectionString = "tcpip = 192.168.0.2: 42424 ", (42424 is the default port of the asp.net Status Service.) Of course, do not forget to install it on the computer 192.168.0.2. and start ASP. NET State Services Service.

StateServer session management

Set the mode attribute to StateServer, that is, store session data to a separate memory buffer, and then control the buffer by running Windows Services on a separate machine. The full name of Status Service is "ASP. NET State Service (aspnet_state.exe), which is configured by the stateConnectionString attribute in the Web. config file. This attribute specifies the server where the service is located and the port to be monitored:

+ Expand

-XML <sessionState mode = "StateServer"
StateConnectionString = "tcpip = myserver: 42424"
Cookieless = "false" timeout = "20"/>

In this example, the status service runs on port 42424 (default port) of a machine named myserver. To change the Port on the server, edit the Port value in the HKEY_LOCAL_MACHINE \ SYSTEM \ CurrentControlSet \ Services \ aspnet_state \ Parameters registry. Obviously, the advantage of using status service is process isolation and can be shared in Web farm. In this mode, session state storage does not depend on the failure or restart of the iis process. However, once the State Service is suspended, all session data will be lost. In other words, the status service does not support persistent data storage as SQL Server does; it only stores data in the memory.

Unable to send a session Status request to the session Status server. Make sure that ASP. NET State Service (ASP. NET State Service) is started and the client port is the same as the server port. If the server is on a remote computer, check

HKEY_LOCAL_MACHINE \ SYSTEM \ CurrentControlSet \ Services \ aspnet_state \ Parameters \ AllowRemoteConnection. If the value is 0 to 1, restart the remote server to make sure the Server accepts remote requests. If the server is located on the local computer and the registry value mentioned above does not exist or is set to 0 (if changed to 1, remote connection is acceptable ), the status server connection string must use "localhost" or "127.0.0.1" as the server name
Store Server Session information in SQL Server
First, let's make some preparations. Start the SQL Server and SQL Server proxy services. Execute a script file named InstallSqlState. SQL in SQL Server. This script file will create a database in SQL Server for storing Session information and an SQL Server proxy job for maintaining the Session information database. You can find the file in the following path:

[System drive] \ winnt \ Microsoft. NET \ Framework \ [version] \
Then open the query analyzer, connect to the SQL Server, open the file and execute it. Wait a moment and the database and job will be created. In this case, you can open the Enterprise Manager and see a new database called ASPState. However, this database only contains some stored procedures and does not use user tables. In fact, Session information is stored in the ASPStateTempSessions table of the tempdb database, and the other ASPStateTempApplications table stores the Application Object Information in ASP. These two tables are also created by the script just now. In addition, you can view "manage"> "SQL Server proxy"> "job" and find another job called ASPState_Job_DeleteExpiredSessions. This job actually deletes expired Session information from the ASPStateTempSessions table every minute.

Then, we return to the Web. config file and change the mode Value to SQLServer. Note: You must also modify the sqlConnectionString value in the following format:

SqlConnectionString = "data source = localhost; Integrated Security = SSPI ;"
Data source refers to the IP address of the SQL Server. If SQL Server and IIS are a Server, write 127.0.0.1. Integrated Security = SSPI means to use Windows Integrated Identity Authentication, so that accessing the database will use ASP.. NET identity, through this configuration, you can obtain better security than the SQL Server authentication method using userid = sa; password = password. Of course, if SQL Server runs on another computer, you may need to maintain consistency between the two sides through Active Directory domains.

Similarly, let's do a test. Add the Session information to SessionState. aspx and you will find that the Session information already exists in SQL Server. Even if you restart the computer, the Session information will not be lost. Now you have fully seen what the Session information looks like and is stored in SQL Server. What you can do depends on your performance. Haha.

Summary
Through this article, you can see that in terms of Session management and maintenance, ASP. NET has made great progress over ASP. We can select a suitable method at will. For enterprise applications, this is undoubtedly beneficial to server synchronization, server stability, and reliability. I believe that with the support of powerful Microsoft, the new generation of e-commerce platforms will be built better!

At the same time, you will also find that the entire technology includes the integration of operating systems, Web Services, and database technologies. I believe that Windows is not Unix stable, IIS is not Apache stable, and SQL Server is not powerful as Oracle. But who can perfectly link them together? So, although Microsoft is not too strong in every aspect, if Microsoft's things are integrated together, who would say that it is not powerful? Microsoft is Microsoft!

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.