1, the session of the storage mode.
Session is actually divided into client session and server-side session.
When a user first establishes a connection with a Web server, the server distributes a SessionID to the user as an identity. SessionID is a 24-character random string. Each time the user submits the page, the browser will include the SessionID in the HTTP header and submit it to the Web server so that the Web server can distinguish which client is the current request page. This sessionid is stored on the client side and belongs to the client session.
In fact, the client session by default is stored in the form of cookies, so when the user disables the cookie, the server side can not get sessionid. We can then use the URL to store the client session. That is, the SessionID is written directly in the URL, of course, this method is not commonly used.
Most of the sessions we mention refer to server-side sessions. He has three ways of storing (custom storage is not discussed here):
1.1 Saved in the IIS process:
Saving in the IIS process means holding the session data in the running process of IIS, which is also the default Inetinfo.exe of the session, and is most commonly used.
The advantages of this approach are simple, the highest performance. However, the session is lost when you restart the IIS server.
1.2. Save on the StateServer
This storage mode refers to storing session data in a separate process, called the ASP.net State service process, that is independent of the ASP.net worker process or the IIS application pool, which ensures that the conversation state is preserved when the Web application is restarted and that session state can be used for multiple web suits on the network The service device.
1.3. Save in SQL Server database
To configure the session data to be stored in a SQL Server database, the programmer first needs to prepare the SQL Server data server and then run it. NET install the state database with the installation tool.
This is still in the server after the reboot because it is stored in memory and disk.
The following is a comparison of these three ways:
|
InProc |
StateServer |
Sql server |
|
Storage physical Location |
IIS process (memory) |
Windows service process (memory) |
SQL Server database (disk) |
|
Storage Type Restrictions |
Unlimited |
Types that can be serialized |
Types that can be serialized |
|
Storage size Limit |
Unlimited |
|
Use Range |
Current request context, for each user independently |
|
Life cycle |
When you first visit a Web site, create a session timeout and destroy it |
|
Advantages |
High performance |
Session not dependent on Web server, not easy to lose |
|
Disadvantages |
Easy to lose |
Serialization and deserialization consume CPU resources |
Serialization and deserialization consume CPU resources, while reading sessions from disk is slower |
|
Principle of Use |
Do not store large amounts of data |
2. Configure session in Web.config
Session configuration information in the Web.config file:
Copy Code code as follows:
<sessionstate mode= "off| inproc| stateserver| SQL Server "
Cookieless= "True|false"
timeout= "Number of minutes"
Stateconnectionstring= "Tcpip=server:port"
sqlconnectionstring= "SQL connection string"
statenetworktimeout= "Number of seconds"
/>
The Mode setting stores session information where:
-off is set to not use the session function;
-inproc set to store session in process, is the ASP storage mode, this is the default value;
The-stateserver is set to store the session in a separate state service;
The-sqlserver setting will store sessions in SQL Server.
Cookieless set up where the client's session information is stored:
-ture uses cookieless mode, where the client's session information is no longer stored using cookies, but is stored through the URL. For example, the Web site is http://localhost/MyTestApplication/(ulqsek45heu3ic2a5zgdl245)/default.aspx
-false uses cookie mode, which is the default value.
Timeout the server automatically discards session information after the number of minutes after the setting. The default is 20 minutes.
stateConnectionString sets the server name and port number used when session information is stored in the status service, for example: "tcpip=127.0.0.1:42424". When the value of mode is StateServer, this property is required. (42424 is the default port).
sqlConnectionString sets the connection string when connecting to SQL Server. For example, "Data source=localhost;integrated security=sspi;initial catalog=northwind". This property is required when the value of mode is SQL Server.
stateNetworkTimeout sets the TCP/IP connection between the Web server and the server that stores the state information when the session state is stored in the StateServer mode and after how many seconds of idle time. The default value is 10 seconds.
here's how to store the session with StateServer and SQL Server
2.1 StateServer
The 1th step is to open the status service. Turn on the control Panel → administrative tools → services command, locate the ASP.net status service item, right-click the service selection startup.
If you formally decide to use the State Service store session before, do not forget to modify the service to start (after the operating system restart service can start itself) so as not to forget to start the service and cause Web session can not be used
Step 2nd, in the system.web node, add: statenetworktimeout= > stateConnectionString represents the Communication address (IP: Service port number) of the state server. Since we are now testing in this machine, set up the cost machine address 127.0.0.1. The State service defaults to a listening port of 42422. Of course, you can also modify the port number of the status service by modifying the registry.
(Modify the registry to modify the port number of the status service: Enter Regedit in run to start Registry Editor-turn on hkey_local_machinesystemcurrentcontrolsetservicesaspnet_ Stateparameters node, double-click Port option-select cardinality to decimal and enter a port number. )
2.2 SQL Server
Executes a script file called InstallSqlState.sql in SQL Server. This script file creates a database in SQL Server to store session information specifically, and a SQL Server Agent job that maintains the session information database. We can find that file in the following path:
[System drive]\winnt\microsoft.net\framework\[version]\
Then open the Query Analyzer, connect to the SQL Server server, open the file just now and execute. Wait a moment, the database and the work is established. At this point, you can open Enterprise Manager, see a new database called ASPState.
Change the value of mode to SQL Server. Note that you also have to modify the value of sqlConnectionString, the format is: sqlconnectionstring= "Data source=localhost; Integrated SECURITY=SSPI; " (This is integrated with Windows authentication)
3, the session life cycle
The lifecycle of the session is actually discussed in the first section, and is related to different stored procedures.
4, Traverse and destroy session
4.1 Traversal:
Copy Code code as follows:
System.Collections.IEnumerator sessionenum = Session.Keys.GetEnumerator ();
while (Sessionenum.movenext ())
{
Response.Write (Session[sessionenum.current.tostring ()). ToString () + "");
}
4.2 Destruction: Session.Abandon ().