Asp.net multi-server session sharing

Source: Internet
Author: User

Requirements:

The company assigned a task. The previous website was a server. because there were too many users and the load was too high, the boss asked to add another server. Add and commit. It should be different from Dev.

Relationship. We should not have met source. However, some data on the website was stored in the session. You should understand the sessino mechanism (your own understanding): when a user accesses the server, a sessionid, httpcontext, will be left in the cookie. current. request. cookies ["ASP. net_sessionid "]. the value can be obtained, and the server will match the data on the server side according to this sessionid. Simply put, your sessionid is used to retrieve the data stored on the server side, similar to key: Word. When a client accesses a server, the data stored in the session is stored on the server. If I add a server, the data of this session will be shared, allow me to access both machines at the same time. In this way, when a user accesses the server, the data in sesson will not be lost when I switch the server machine.

Procedure

I searched on the Internet about the session sharing method, there are a lot of http://www.cnblogs.com/xinhaijulan/archive/2010/08/21/1805116.html, I chose a sqlserver sharing sessin method.

1.

First, you have to create this sqlserver database. CMD command. C: \ windows \ microsoft. Net \ framework \ v2.0.50727> aspnet_regsql.exe-s jpmsnmdb05t-e-d awbuisession-ssadd-sstype C

The command does not understand the link in my article. Very simple.

Or run installpersistsqlstate. SQL (Create Database) uninstallpersistsqlstate. SQL (uninstall) in c: \ windows \ Microsoft. NET \ framework \ v2.0.50727)

2.

Then, enter <system. Web>

Link to the tag if you do not understand it

<machineKeyvalidationKey="1234567890123456789012345678901234567890AAAAAAAAAA"decryptionKey="123456789012345678901234567890123456789012345678"validation="SHA1"decryption="Auto"/>
<sessionStatemode="SQLServer"sqlConnectionString="server=jpmsnmdb05t; database=AWBUISession;Trusted_Connection=Yes; Persist Security Info=False;"allowCustomSqlDatabase="true"cookieless="false"timeout="100"/>

Then, in the program, perform sesssion ["test"] = "test value" or take the session content and check it. OK, the session data will be put into the database I created.

Note:

  If the configuration above is not successful, I can only say that you must have made a mistake. Please refer to the following comparison:

1. the value written to the session must be serializable (I encountered this problem and killed me. I didn't understand it at the beginning. I just said a note on the Internet, but I didn't pay attention to serialization, cry when the configuration fails.) Let me give you an example. For example, if you create a class user, when you sesssion ["user"] = new user ("name", "PWD"); you must remember that the user class must be serialized, otherwise you will never succeed. How to serialize, simply add a sentence [serializable ()] to the public class user. remember that all the values must be serialized, as long as you set values in the session, because the system will serialize the values you write into the session and store them in the aspstatetempsessions table.

2. I configured my own test environment, in the http://www.cnblogs.com/xinhaijulan/archive/2010/08/21/1805116.html I also asked others how to configure, at that time I do not know whether the test environment I created is correct.

My configuration environment (the following is what I ask someone else ):

I don't know if the landlord can see it. It's a long time.
I configure it in your way. I want to share sessions with multiple servers and use SQL Server. I don't know if my test method is correct. I set the local host to 172.23.126.137 sessiontest # source server 10.168.109.120 sessiontest # X client host
I put my code on these two servers, and then I stopped one server, IIS, and then accessed http: // sessiontest: 8078/For example, the user login function, it adds a record to sqlserver. Then I open this one, stop the other, and access the URL. It is still the user login page. It should be based on the principle that the user information is successfully checked and the user login will not be performed again. Both servers use port 8078 for IIS, but the IP address is different. Hope you can see it ....... although the landlord answered my question, I still did not understand it, but I am very grateful to the friend. After two or three days, I still found the cause of the problem: I want to raise a question, the sessionid in the aspstatetempsessions table is actually sessionid + appid (8 digits ). appid is the hexadecimal representation of appid in the aspstatetempsessions table. I read a lot of information online. What is the appid IN THE aspstatetempsessions table. Accidentally, my tester reminded me that I took a screenshot, as shown below, and 26 was not displayed. The following table lists my aspstatetempsessions tables. Appid appname-1977566844/lm/w3svc/26/root

You can set the ID to the same in multiple servers. If you have any questions, you can ask me, I am working on this thing recently.

 

 

  

 

 

 

 

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.