In-depth analysis of Session Server Configuration Guide and user experience

Source: Internet
Author: User
This article provides a detailed analysis of the Session Server Configuration Guide and user experience. For more information, see

This article provides a detailed analysis of the Session Server Configuration Guide and user experience. For more information, see

I. Summary
All Web programs use Session to save data. the independent Session server can solve the Session Sharing Problem in the Server Load balancer scenario. this article introduces. NET platform, and introduces various experiences and skills in using Session.

Ii. Session, SessionID, and Cookies
Session data is stored on the server, but each client needs to save a SessionID, which is stored in Cookies and expires when the browser is closed.

The HTTP request sent to the server contains the SessionID. The server obtains the Session information of this user based on the SessionID.
Many junior developers do not know the relationship between SessionID and Cookies. this is incorrect. it is because SessionID is stored in Cookies that, when we save Cookies, be sure not to cause the SessionID object because of the size and number of Cookies. in our program, Cookies of SessionID are specially processed:

The Code is as follows:


///


/// Write cookie.
///
///
///
Public bool SetCookie (int day)
{
String CookieName = GetType (). ToString ();
HttpCookie SessionCookie = null;
// Backup SessionId.
If (HttpContext. Current. Request. Cookies ["ASP. NET_SessionId"]! = Null)
{
String SesssionId = HttpContext. Current. Request. Cookies ["ASP. NET_SessionId"]. Value. ToString ();
SessionCookie = new HttpCookie ("ASP. NET_SessionId ");
SessionCookie. Value = SesssionId;
} // Omit the middle part of the code. Only the logic for backing up the SessionID and retrieving the SessionID is retained.
// If the total number of cookies exceeds 20, rewrite ASP. NET_SessionId to prevent Session loss.
If (HttpContext. Current. Request. Cookies. Count> 20 & SessionCookie! = Null)
{
If (SessionCookie. Value! = String. Empty)
{
HttpContext. Current. Response. Cookies. Remove ("ASP. NET_SessionId ");
HttpContext. Current. Response. Cookies. Add (SessionCookie );
}
}
Return true;
}


Iii. Several Methods for setting up a Session Server
By saving sessions on an independent server, you can share sessions among multiple Web servers. although we can also develop the Session storage system by ourselves, we can use ASP. NET built-in storage mechanism will be more convenient.
. NET provides five ways to save the Seission:

Method Name

Storage Performance

Off

Set to not use the Session Function

None

InProc

Set the Session to be stored in the process, that is, the ASP storage method. This is the default value.

Highest performance

StateServer

Set to store sessions in independent State services. Usually the aspnet_state.exe process.

Performance Loss: 10-15%

SQLServer

Set to store sessions in SQL Server.

Performance Loss: 10-20%

Customer

Custom Storage Solution

Determined by the implementation method

We can. the Session storage method used by the configuration program in Config. by default, InProc is saved in the IIS process. this article does not explain Off, InProc, and Customer. you can search related articles online.
The following describes the applications of StateServer and SQLServer.

4. Set up a Session server in StateServer mode
(1) Server Configuration
1. Start the Asp.net State service. (the default status of this service is manual. Change it to automatic and start .)
2. Modify the Registry: [HKEY_LOCAL_MACHINE \ SYSTEM \ ControlSet001 \ Services \ aspnet_state \ Parameters]
Set AllowRemoteConnection = 1 and Port = 42424 (decimal, 42424 by default)
Port is the service Port number.
AllowRemoteConnection indicates whether to allow connections from other machines. 0 indicates that the connection can only be used by the local machine, and 1 indicates that the connection can be used by other machines.

(2) Client settings
In the Web. Config of the Web application, we need to modify / Of Node. If no
If not, add (the InProc method is used by default)

The Code is as follows:


Mode = "StateServer"
StateConnectionString = "tcpip = Server ip Address: 42424"
Cookieless = "false"
Timeout = "60"/>


The above parameters can be modified as needed.

5. Set up a Session server in SqlServer Mode
(1) Server Configuration
There are two ways to set up the Session server using SqlServer mode. for ASP. NET 1.0 and 1.1, use a and 2.0, that is, use B.

A. Create a Session database using an SQL File
In ASP. NET 1.0 and 1.1, you can only use this formula. For example, use the aspnet_regsql.exe tool. (Of course, this method is also applicable to version 2.0)
. Net provides the database installation script, which can be found in the windows folder of the machine:
C: \ WINDOWS \ Microsoft. NET \ Framework \ v2.0.50727 \ InstallSqlState. SQL
C: \ WINDOWS \ Microsoft. NET \ Framework \ v2.0.50727 \ InstallSqlStateTemplate. SQL
Depending on the version of ASP. NET, you need to use different SQL scripts. ASP. NET mainly has two versions: 1.1 and 2.0. You can find these two SQL statements in different version folders.
InstallSqlState. SQL is the database with the default name "[ASPState]". This SQL can be run directly.
InstallSqlStateTemplate. SQL can save data using the database you specified. This SQL needs to be modified and run. Open the SQL file and replace [DatabaseNamePlaceHolder] with the database name you specified.
You do not need to specify a database when executing installsqlstate. SQL, which can be executed on any database. This SQL statement creates a new database by yourself.

B. Use the aspnet_regsql.exe tool.
After ASP. NET 2.0133, The aspnet_regsql.exe tool is soft enough to easily configure the Session database. This tool is located in the "system root directory \ Microsoft. NET \ Framework \ version" folder on the Web server.
Example:
Aspnet_regsql.exe-S.-U sa-P 123456-ssadd-sstype p
-S parameters:
Indicates the database instance name. You can use "." to indicate the local machine.
-U and-P parameters:
Indicates the user name and password.
-E parameter:
You can select a group in-U-P and-E.-E indicates that the current system user logs on to the database through windows authentication, and-U-P indicates that the SQL Server user logs on to the database.
-Ssadd/-ssremove parameters:
-Ssadd indicates that the Session database is added, and-ssremove indicates that the Session database is removed.
Sstype parameters:

Option

Description

T

Store session data in the SQL Server tempdb database. This is the default setting. If session data is stored in the tempdb database, the session data is lost when SQL Server is restarted.

P

Store session data in the ASPState database instead of the tempdb database.

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.