SQL Server session sharing attention points

Source: Internet
Author: User
Tags httpcontext

Company mission, before the site is a server, due to too many users, the load is too large, now the boss asked to add a server. Plus, gabexate, should have been with me, Dev.

Relationship, should not touch the source of it. However, the previous site has some data that is placed in the session. Everyone should understand the Sessino mechanism (own understanding): When the user accesses the server, it will leave a sessionid,httpcontext.current.request.cookies["Asp.net_sessionid" in the cookie. Value can be taken, and then the server will be based on this sessionid to match my server side of the data, simply to take your sessionid to take the data stored on my server, similar to Key:word. Because when a client accesses a server, the data stored in the session is on the server side, if I add a server, the session of the data will be shared, can let me two machines simultaneously access. In this way, when the user accesses the server, I switch the server machine to not lose the data inside the Sesson.

The problem I encountered:

In the online search on the session sharing method, http://www.cnblogs.com/xinhaijulan/archive/2010/08/21/1805116.html there are many, I will choose a method that SQL Server shares Sessin.

First, you have to build this SQL Server 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 text. Very simple.

Then, in the Web. config in the ASP. NET Project <system.web> input

<machinekeyvalidationkey= "1234567890123456789012345678901234567890AAAAAAAAAA" decryptionkey= " 123456789012345678901234567890123456789012345678 "validation=" SHA1 "decryption=" Auto "/><sessionstatemode=" SQL Server "sqlconnectionstring=" SERVER=JPMSNMDB05T; Database=awbuisession; Trusted_connection=yes; Persist Security info=false; " Allowcustomsqldatabase= "true" cookieless= "false" timeout= "" "/>

At this point you set session["name"]= "value" in your code, and he will keep your information in the session table in the database you created in SQL Server.

Read the words, I was set up, from the client to get SessionID, and then according to this sessionid to the table inside the query, the data found to decompile it can be, do not know how to see my attachment, I wrote a user login test system.

One of the problems I encountered was that I didn't build a test environment. The light has two machines. You have to remember that SessionID is a cookie information, then different domain (is the URL) his sessionid is different, so you want to build multiple servers to share a session, it is necessary to establish a common domain. So you can access the server as you like, for example: when the user logged in to access the first server, log in good, the server switched to the second server, the user of course do not know, the user still continue to visit the page, the system must check whether the user is logged in, or let the user login to, good, You have to look at the information in the session, according to the SessionID get information? If the domain is different, your sessionid is different, now your SessionID is not the previous one, you must be nostalgic for your login to the first server SessionID. If you set the same domain, well, no matter how many machines you visit, your SessionID is one, then it is not a small case to take out the user information. Well, how to build an identical Domian:

Set local host, where is the local host path? I asked my colleague, and I'm not quite sure, C:\Windows\System32\drivers\etc.

Very simple, you know your server IP address. So just modify your host file to see

172.23.126.137 sessiontest # source Server
10.168.109.120 Sessiontest # x client Host

This is the one I added, two servers, the same domain:sessiontest

Since I posted the system to server IIS, the port number was set at 8078, so I just need to access http://sessiontest:8078/locally.

Manually switch the server on/off in IIS inside the site.

The environment for this test is also in place.

There is an unresolved problem, if there are readers involved can also exchange, run my vs in the local, visit the page, no problem,

If I put the system on the server, I found that when I take the session information, the session database table to meet my page search conditions only one, but it appears two, these two SessionID, the former more than half is the same, With httpcontext.current.request.cookies["Asp.net_sessionid"]. Value is the same. httpcontext.current.request.cookies["Asp.net_sessionid"]. Value is not the same as the SessionID in my aspstatetempsessions table. My httpcontext.current.request.cookies["Asp.net_sessionid"]. Value is only half the SessionID in the ASPStateTempSessions table. See

httpcontext.current.request.cookies["Asp.net_sessionid"]. Value : x0if3uuddqmpl4451jberf2h

SessionID in ASPStateTempSessions table: x0if3uuddqmpl4451jberf2h077b2520

SQL Server session sharing attention points

Related Article

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.