Asp.net implements multi-site session sharing

Source: Internet
Author: User

Most programs are developed tomorrow. Due to session and cookie permissions problems, it is very difficult to share a session or cookie on multiple sites. Today, let's look at a sharing session method, for more information, see.

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: WindowsMicrosoft. NETFramework64v4.0.30319

 

Run in this directory:
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:

The Code is as follows: Copy code

ALTERPROCEDURE [dbo]. [TempGetAppID]
@ AppName tAppName,
@ AppIdint OUTPUT
AS
SET @ appName = LOWER (@ appName)
SET @ appId = NULL

SELECTtop1 @ appId = AppId
FROM [SNSSessionDB]. dbo. ASPStateTempApplications
-- WHERE AppName = @ appName

IF @ appIdISNULLBEGIN
BEGINTRAN

SELECT @ appId = AppId
FROM [SNSSessionDB]. dbo. ASPStateTempApplications WITH (TABLOCKX)
WHERE AppName = @ appName

IF @ appIdISNULL
BEGIN
EXEC GetHashCode @ appName, @ appId OUTPUT
INSERT [SNSSessionDB]. dbo. ASPStateTempApplications
VALUES
(@ AppId, @ appName)

IF @ ERROR = 2627
BEGIN
DECLARE @ dupApp tAppName

SELECT @ dupApp = RTRIM (AppName)
FROM [SNSSessionDB]. dbo. ASPStateTempApplications
WHERE AppId = @ appId

RAISERROR ('SQL session state fatal error: hash-code collision between applications' % s' and '% s ''. please rename the 1st application to resolve the problem. ',
18, 1, @ appName, @ dupApp)
END
END

COMMIT
END

RETURN0

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

The Code is as follows: Copy code

<HttpCookies domain = "news.com"/>
<SessionState mode = "SQLServer" sqlConnectionString = "data source = [Server]; initial catalog = [DataBase]; user id = [UserName]; password = [Password] "allowCustomSqlDatabase =" true "timeout =" 120 "/>

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.

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.