Session storage and Configuration Methods

Source: Internet
Author: User

1. Session storage method.

Sessions are divided into client sessions and Server sessions.

When a user establishes a connection with the Web server for the first time, the server will distribute a SessionID to the user as the identifier. SessionID is a random string consisting of 24 characters. Each time a user submits a page, the browser will include this SessionID in the HTTP header and submit it to the Web server, so that the Web server can distinguish the client of the current request page. This SessionID is stored on the client and belongs to the Client Session.

In fact, client sessions are stored in the form of cookies by default. Therefore, if the cookie is disabled, the server will not receive the SessionID. In this case, we can use the url method to store client sessions. That is, the SessionID is directly written in the url. Of course, this method is not commonly used.

Most of the sessions we mentioned refer to server-side sessions. There are three storage methods (custom storage is not discussed here ):

1.1 save in IIS process:

The iisimport process stores sessiondata in the iisruntime process and inetinfo.exe processes. This is also the default Session storage method and the most common method.

This method has the advantages of being simple and having the highest performance. However, Session loss occurs when the IIS server is restarted.

1. 2. Save it on StateServer

This storage mode refers to storing Session data in an Asp. net status service process, the process is independent of Asp.. Net or a separate process in the IIS application pool. This mode ensures that the session status is retained when the Web application is restarted, the session status can be used on multiple Web servers in the network.

. Save in SQL Server database

You can configure the Session data to be stored in the SQL Server database. To perform this configuration, the programmer first needs to prepare the SQL Server data Server and then install the status database in the. NET Installation tool.

This method remains after the server is suspended and restarted, because it is stored in the memory and disk.

The following is a comparison of the three methods:


InProc

StateServer

SQLServer

Storage physical location

IIS process (memory)

Windows service process (memory)

SQLServer database (Disk)

Storage type restrictions

Unlimited

Serializable types

Serializable types

Storage size limit

Unlimited

Scope of use

The current request context, which is independent of each user

Lifecycle

The first time you access the website, the created Session times out and is destroyed.

Advantages

High Performance

Sessions are not dependent on Web servers and are not easy to lose

Disadvantages

Easy to lose

Serialization and deserialization consume CPU resources

Serialization and deserialization consume CPU resources and slow Session reading from disk

Usage principles

Do not store large amounts of data

2. Configure the Session in web. config.
Session configuration information in the Web. config file:Copy codeThe Code 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 mode setting stores Session information:
-Off is set to not use the Session function;
-InProc is set to store sessions in the process, that is, the storage method in ASP. This is the default value;
-Set StateServer to store sessions in independent State services;
-SQLServer sets to store sessions in SQL Server.
  
Cookieless sets where the Session information of the client is stored:
-Ture uses the Cookieless mode. In this case, the Session information of the client is no longer stored using cookies, but stored through URLs. For example, the URL is http: // localhost/MyTestApplication/(ulqsek45heu3ic2a5zgdl245)/default. aspx
-False: Cookie mode. This 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. (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 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.

  The following describes how to use StateServer and SqlServer to store sessions.

2.1 StateServer
Step 2 is to open the status service. Open the "control panel"> "Administrative Tools"> "services" command, find the ASP. NET status service item, right-click the service, and choose start.
If you officially decide to use the status service to store sessions, do not forget to change the service to self-start (the service can be started after the operating system is restarted) so that you do not forget to start the service and the website Session cannot be used.
Step 2: Add stateNetworkTimeout = "20"> stateConnectionString to the system. web node to indicate the communication address (IP: Service port number) of the Status server ). Because we are now testing on the local machine, set the cost machine address 127.0.0.1 here. The default listening port of Status Service is 42422. 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 at run to start the Registry Editor-open the HKEY_LOCAL_MACHINESYSTEMCurrentControlSetServicesaspnet_stateParameters node in turn, double-click the Port option-select base in decimal, enter a port number .)

2.2 SqlServer
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.
Change the mode Value to SQLServer. Note: You must also modify the sqlConnectionString value in the format of sqlConnectionString = "data source = localhost; Integrated Security = SSPI;" (this is achieved through windows Integrated authentication)

  3. Session Lifecycle
The Session lifecycle has actually been discussed in section 1, which is related to different stored procedures.

 4. traverse and destroy sessions
4.1 traversal:Copy codeThe Code is as follows: System. Collections. IEnumerator SessionEnum = Session. Keys. GetEnumerator ();
While (SessionEnum. MoveNext ())
{
Response. Write (Session [SessionEnum. Current. ToString ()]. ToString () + "");
}

4.2 destroy: Session. Abandon ().

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.