Ahsan
Ahsan

Reputation: 271

How to maintain same session in two different sites

let suppose i have two domains

1. abc.com
2. xyz.com

now the thing i want to do that i have a index page on both sites and there is a image on both index page , but when i click next in abc.com and image changes on abc.com at the same time i made a session variable in sql server . Now leave abc.com and come to xyz.com the index page of xyz.com automatically gets refresh by ajax function after 2 second , now when page get refresh it will make a request to server and pick the next image name from session which we stored using abc.com and by getting the we will show the latest image on xyz.com .... Note . both sites are using the same server

can i do this ? . If yes then how ?

Upvotes: 1

Views: 3883

Answers (1)

Andy
Andy

Reputation: 6466

You can share sessions between sites using SQL server as the session manager, I did it following these instructions, works well.

these steps are taken from: Share ASP.net session between domains

ASP.NET APPLICATION : CSASPNETShareSessionBetweenSubDomains Project Overview

Summary:

Session can be set to different modes (InProc, SqlServer, and StateServer). When using SqlServer/SateServer mode, Session will store in a specific SQL Server/Sate Server. If two ASP.NET Web Applications specify the same SQL Server as Session Server, all Sessions store in the same database. All in all, if using SQL Server Session, it is possible to share Session between different ASP.NET Applications. Since ASP.NET stores Session Id to cookie to specify current Session, so in order to share Session, it is necessary to share Session Id in the cookie.

The CSASPNETShareSessionBetweenSubDomains sample demonstrates how to configure a SessionState Server and then create a SharedSessionModule module to achieve sharing Session between sub domain ASP.NET Web Applications.

Two ASP.NET Web Applications need to run in the same Root Domain (can use different ports). Steps:

  1. Configure SQL Server to Store ASP.NET Session State.

    Run "C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_regsql.exe -S localhost\sqlexpress -E -ssadd" to add Session State support to Sql Server Express 1.

    If you haven't added Session State to SQL Server, when you configure a web site to use SQL Server Mode Session State, System.Data.SqlClient.SqlException will be thrown saying "Invalid object name 'tempdb.dbo.ASPStateTempSessions'."

  2. Configure ASP.NET Web Applications to Use SQL Server to Store Session and Use specific decryptionKey and validationKey.

    Add this settings to web.config file to use SQL Server Session State:

    Add this settings to web.config to use specific decryptionKey and validationKey:

    If you host the applications in IIS, please run the Application Pool under an account who can log into the database. Otherwise System.Data.SqlClient.SqlException will be thrown saying "Cannot open database 'ASPState' requested by the login. The login failed."

  3. Write SharedSessionModule Module to Achieve The Logic of Sharing Session

    a. Implement Init() method to set Application Id read from web.config.

    b. Implement PostRequestHandlerExecute Event to store Session Id to cookie with the same domain and root path.

  4. Configure ASP.NET Web Applications to Use SharedSessionModule Module. Add this config to web.config to use SharedSessionModule Module:

    If you run the applications in your own domains except localhost,
    please don't forget to change the value of RootDomain after publishing.

  5. Run and Test a. Add a new Web Page. b. Add two Buttons (used to Refresh the page and Set Session) and one Label for displaying Session value. c. On Page_PreRender() method, read Session and display it in Label. On Button Click Event, Set Value to Session. d. Create a new Web Site with the same config as Web Site 1, but set different value to Session e. Now open two sites in two tabs. Now if you set Session Value in site1, you can retrieve the same value in site2. So they use the same Session.

1 Remove Session State from Sql Server. Run "C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_regsql.exe -S localhost\sqlexpress -E -ssremove" to remove Session State support from Sql Server.

Upvotes: 3

Related Questions