For most websites, session is used to maintain user operations in one session; session is essential for any web project (except for the website that does not contain any user operations, ^ _ ^ this is unlikely for websites in the web era ). For a single site, it is okay to use only one session for a single site. But for multiple sites at the same time, how can we achieve session sharing for multiple sites?
Common practices include:
- Use cookies to share multiple sites (this method is only applicable when multiple sites are under the same domain name );
- Use. Net Automatic Status Service (Asp.net State Service );
- Use the. NET session database;
- Use memcacheddb.
The above solutions have their own advantages. Where are the advantages and disadvantages of each method? It would be hard to say clearly at half past one. If there is an article that can be explained clearly, it is all nonsense. This kind of things is not used in practice. simply say that this solution is good and that solution is not good, it's all nonsense.
Our project is now using the third solution, which uses the session database to solve multi-site session sharing. If you have different opinions, you can talk about the implementation method below:
- Now that you are using the session database, you must first create the database. Now that you are using the Microsoft solution, Microsoft will certainly provide you with corresponding implementation methods. Use the command line to enter:
1 C:\Windows\Microsoft.NET\Framework64\v4.0.30319
Run in this directory:
1 aspnet_regsql.exe-sstype C-ssadd-d your database name-u username-P password-s Database Service address
Note: The parameters here are case-sensitive. After the preceding command is executed, the session database is created. After creation, you can open the database to check whether the database is created successfully.
- When the session database is successfully created, it does not mean that the session can be shared for multiple purposes. You also need to make some small operations on the current session database to trick the database into saying "hey, I am running an application. ^_^ ". Let's take a look at how to do this:
1ALTERPROCEDURE[dbo].[TempGetAppID] 2@appName tAppName, 3@appIdint OUTPUT 4AS 5SET@appName=LOWER(@appName) 6SET@appId=NULL 7 8SELECTtop1@appId= AppId 9FROM[SNSSessionDB].dbo.ASPStateTempApplications 10--WHERE AppName = @appName11 12IF@appIdISNULLBEGIN 13BEGINTRAN 14 15SELECT@appId= AppId 16FROM[SNSSessionDB].dbo.ASPStateTempApplications WITH (TABLOCKX) 17WHERE AppName =@appName 18 19IF@appIdISNULL 20BEGIN 21EXEC GetHashCode @appName, @appId OUTPUT 22 23INSERT[SNSSessionDB].dbo.ASPStateTempApplications 24VALUES 25 (@appId, @appName) 26 27IF@@ERROR=2627 28BEGIN 29DECLARE@dupApp tAppName 30 31SELECT@dupApp=RTRIM(AppName) 32FROM[SNSSessionDB].dbo.ASPStateTempApplications 33WHERE AppId =@appId 34 35RAISERROR('SQL session state fatal error: hash-code collision between applications ''%s'' and ''%s''. Please rename the 1st application to resolve the problem.', 3618, 1, @appName, @dupApp) 37END 38END 39 40COMMIT 41END 42 43RETURN0
In fact, I only made a few changes, that is, adding a top 1. Each time I check, I can only use the appid for the first time. That is to say, I only have one session instance for multiple projects.
All the above methods have been done. In fact, only one purpose is to use the session database in the project (complain: Let's say that Microsoft is a fool, but it is not false at all, he thought of everything for you. We can simply use him, and this is also a generation. the tragedy of NET developers. Every time Microsoft has made new technological changes, our hard-pressed developers will learn and learn. Then Microsoft suddenly said that I would not upgrade the technology any more, I want to give up on him. Well, you can see, there are bitter faces on the street, and half of them are engaged in development .). How can I use it in a project? Modify web. config add or modify the following items in system. Web
1
4. OK, you are done. Remember to restart IIS and clear the cookies or junk items on your local machine. This will have a better effect.
Through the above series of operations, we finally got OK. When using it in a project, just as we usually do, it's okay to assign values and call values.
OK. The write is finished.