Http://blog.csdn.net/lvlingwy/archive/2008/05/08/2418673.aspx
Http://bbs.ibeifeng.com/simple/index.php? T16926.html
From: http://sai5d.blog.sohu.com/131936651.html
In the WEB field, dynamic WEB pages often cause SESSION loss due to loads on several hosts. There are also many introductions on the Internet, here I will share with you the process I have experienced:
The system must run in a load balancing Web environment, and the system configuration file web. the Session Status in config is set to InProc (that is, the Session status is stored locally), which leads to the situation where the Session often times out when the user's access volume is large. This is mainly because the user accesses the WEB application system through the load balancing IP address, and saves the Session Status of a Session on a server at a certain time, however, the Session Status of the Session is not saved on other WEB Front-end servers. As the concurrency increases, the load balancing function can be used as a route to access idle servers at any time, as a result, the idle server does not have the previously saved Session status.
Solution:
1. when you run ASP. NET Web applications must use SqlServer or StateServer Session Status mode. In the project, we did not select SqlServer mode to store Session status based on performance considerations, instead, select a SessionStateServer server to view the Session Status of the user. We need to set the following in the System Configuration File web. config:
<SessionState mode = "StateServer" cookieless = "false" timeout = "240" stateConnectionString = "tcpip = 192.168.0.1: 42424" stateNetworkTimeout = "14400"/>
Here, the red-font IP address must be a machine in the same domain. On this machine, perform Step 2 operations, in the registry, set HKEY_LOCAL_MACHINE \ SYSTEM \ CurrentControlSet \ Servi ces \ aspnet_state \ Parameter
The key value of s \ AllowRemoteConnection is changed to 1, and then the ASP. NET State Service of the local machine is restarted.
Add another item
<MachineKey validationKey = "Hangzhou" decryptionKey = "5FC88DFC24EA123C" validation = "SHA1"/>
How to generate a machineKey?
According to the MSDN standard, "The key is configured to encrypt and decrypt Forms authentication Cookie data and view status data, it is used to verify the non-process session status identity." That is to say, many Asp. Net encryption relies on values in machineKey, such as Forms authentication Cookie and ViewState encryption. By default, Asp. net configuration is dynamically generated by yourself. If a single server is okay, but if multiple servers are load balanced, machineKey is also dynamically generated. The machinekey values on each server are inconsistent, as a result, the encrypted results are inconsistent and the verification and ViewState cannot be shared. Therefore, you must configure the same machineKey for Server Load balancer instances on each site.
Algorithm generated by machineKey:
ValidationKey = CreateKey (20 );
DecryptionKey = CreateKey (24 );
Protected string CreateKey (int len)
{
Byte [] bytes = new byte [len];
New RNGCryptoServiceProvider (). GetBytes (bytes );
StringBuilder sb = new StringBuilder ();
For (int I = 0; I <bytes. Length; I ++)
{
Sb. Append (string. Format ("{0: X2}", bytes [I]);
}
Return sb. ToString ();
}
See matchineKey configuration for reference:
<? Xml version = "1.0"?>
<Configuration>
<System. web>
<MachineKey validationKey = "3FF1E929BC0534950B0920A7B59FA698BD02DFE8" decryptionKey = "encrypt" decryption = "3DES" validation = "SHA1"/>
</System. web>
</Configuration>
2. we also need to start ASP on the SessionStateServer server. NET State Service, specific settings: Control Panel> Management Tools> services> ASP. NET State Service, set it to automatic start.
3. Microsoft "Internet Information Service" (IIS) settings for each front-end WEB Service
To maintain the session status between different Web servers in the Web farm, Microsoft "Internet Information Service" (IIS) configures the Application Path of the Web site in the database (for example, \ LM \ W3SVC \ 2) must be the same as all Web servers in the Web field. The application path must be case sensitive. On a Web server, the instance ID of the Web site that hosts ASP. NET Applications may be 2 (where the application path is \ LM \ W3SVC \ 2 ). On another Web server, the instance ID of the Web site may be 3 (where the application path is \ LM \ W3SVC \ 3 ). Therefore, the application paths between Web servers in the Web field are different. We must make the instance IDs of the Web site the same. You can save a WEB configuration as a file in IIS. the IIS configuration of other Web servers can come from this file. If you want to know the specific settings, visit the Microsoft Support Website: http://support.microsoft.com/default.aspx? Scid = kb; zh-cn; 325056
Additional information:
PRB: Session Variables Are Lost If You Use FRAMESET in Internet Explorer 6.0
Http://support.microsoft.com/kb/323752/EN-US /#
PRB: Session Data Is Lost When You Use ASP. NET InProc Session State Mode
Http://support.microsoft.com /? Id = 324772
PRB: If you use SqlServer or StateServer session mode, the Web field will lose the session status.
Http://support.microsoft.com/default.aspx? Scid = kb; zh-cn; 325056
ASP. NET Session State FAQ
Http://www.eggheadcafe.com/articles/20021016.asp
Reference from: http://hi.baidu.com/panshuaiyang
How can an application store Session information in two ways?
The following operations are mainly for 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 go back to the SessionState. aspx page to check the Session information. We found that the Session information was not lost.
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 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 ". Of course, do not forget to install. NET Framework on the 192.168.0.2 computing machine and start the ASP. NET State Services Service.
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!
From: http://topic.csdn.net/t/20050314/17/3850093.html