Multiple Sites share sessions and set the Session expiration time

Source: Internet
Author: User

Multiple Sites share sessions and set the Session expiration time

At work, when multiple sites need to share the Session value and find a lot of relevant content, either it is too difficult to understand or it is too troublesome. Later, I finally finished it. I wrote it down first, maybe it will be used later.

Development language: C #
Tool: VS2008 Simplified Chinese version (I changed the English version, the English version is not very good ......)
Database: SQL Server 2005
Running platform: Windows Server 2003sp2 iis6.0

When multiple sites need to share the Session value, the premise is that all sites belong to the same top-level domain name, such as aaa.123.com and bbb.123.com or more. Should. net Framework 2.0 or a later version be installed?

First, add the following configuration in web. config:

<System. web> <! -- Add the following sessionState to the system. web node -->
<SessionState
Cookieless = "false"
<! -- If cookieless is set to true, the sessionid will be automatically added on the link. It is not related to the user. It is automatically added by iis. -->
RegenerateExpiredSessionId = "true" mode = "SQLServer" timeout = "20" <! -- Set the Session expiration time -->
<! -- Mode = "SQLServer" indicates creating a Session in SQLServer Session mode (your own understanding) -->
SqlConnectionString = "data source = 127.0.0.1; uid = sa; password =">
<! -- SqlConnectionString: This is the string connecting to the database. We recommend that you do not use SQLEXPRESS for the database instance, which will be mentioned later -->
</SessionState>
</System. web>

There is also a writing method with higher security:

<SessionState
Cookieless = "true" timeout = "20" <! -- Set the Session expiration time in minutes -->
RegenerateExpiredSessionId = "true" mode = "SQLServer" sqlConnectionString = "data source = ..; Integrated Security = SSPI;"> <! -- SSPI indicates logon using Windows Authentication mode -->
</SessionState>

Next, you need to enable the SQL server job function. If SQLEXPRESS is used, you cannot see the SQL server job function, that is, why I didn't recommend using SQLEXPRESS as an instance, that is to say, if it is a local test, directly Press ". "Add the user name and password to log on to SQLServer. For more information about how to enable SQL Server jobs, see SQL Server jobs.
Log on to SQLServer. If the SQLServer job is not started, start it first and find "InstallSqlState. SQL file, located in "C: \ WINDOWS \ Microsoft. NET \ Framework \ v2.0.50727 \ "directory.
"V2.0.50727" is the Framework version, and SQLServer2005 uses. net Framework2.0. Therefore, only 2.0 contains this file: InstallSqlState. SQL.
After the InstallSqlState. SQL file is executed, an AspState database is created in the database, but there are no tables, but many stored procedures are added. There are also jobs (this is why you need to enable the job function first without using the SQLExpress instance. The job function is to check two tables in Tempdb one minute and delete some expired Session values, otherwise, the Session value will always be stored in the database ).
Two temporary tables are added to Tempdb.
ASPStateTempApplications
ASPStateTempSessions
Two errors may be prompted here:

One is:
Message 14261, level 16, status 1, process sp_add_category, 26th rows
The specified @ name ('[Uncategorized (Local)]') already exists.
This is not clear about the usage. If it is wrong, no exception is found, so no matter what it is.
Another one is:
This may cause a service not enabled (SQL Server Agent) required by the job to be manually enabled on the Server and the connected peripheral application configurator, it is best to set it to automatic run directly.

All the preparation work is completed. It is best to check whether the job has started to run correctly. If not, manually configure the job and run it again to ensure that the SQL Server session is allowed normally. If you are patient, create two web sites in iis and then file C: \ WINDOWS \ system32 \ drivers \ etc \ hosts (open it in Notepad)
Add a new line after "127.0.0.1 localhost"
127.0.0.1 aaa.123.com
127.0.0.1 bbb.123.com
(The role is to simulate the domain name effect. Session sharing must be under the same top-level domain name)
The web. config is effective only when it is configured in the same way as the configuration file mentioned at the beginning, and the site outputs Session values. Also, do not forget to create a mutual access link for ease of viewing, configure the Host header of the site as aaa.123.com and bbb.123.com. When you open the site, you will find that the Session value has been shared. When the test is over, do not forget to clear the stuff that has just been simulated by the domain name. Otherwise, the browser will not be able to access the real websites of the two domain names you set up in the future ......

In the end, Microsoft is very good and powerful ......

Read the full text

Category:Net view comments

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.