Click here to Skip to main content
11,719,973 members (83,126 online)
Click here to Skip to main content

Sharing ASP.NET Web Application's Session State

, 22 Apr 2013 CPOL 13.3K 10
Rate this:
Please Sign up or sign in to vote.
Learn how to share ASP.NET sessions on different websites


You can easily learn how to share session states in ASP.NET from different websites on different domains.

Using the Code

The first thing you need to do is to configure your Aspstate database. Open cmd.exe as administrator and write this command (modify it if necessary) and execute it:

-S [Server] -E -sstype c -d [Database] -ssadd

After that, modify TempGetAppID Stored Procedure as below:


@appName tAppName,
@appId int OUTPUT
SET @appName = LOWER('King Session State')
SET @appId = 1

SELECT @appId = AppId
FROM dbo.ASPStateTempApplications
WHERE AppId = 1

        INSERT dbo.ASPStateTempApplications
        VALUES (@appId, @appName)


Execute it to make sure all websites have the same AppId in which sessions will be shared.

Now modify web.config and add these lines to it:

    <sessionState mode="SQLServer" allowCustomSqlDatabase="true" 
    sqlConnectionString="Data source=.;Initial Catalog=SessionDB;User ID=???;
    Password=***" timeout="60" />

From now the ApplicationId of your sites will be same & your session states will be shared in all websites on the current domain. (in sub domains). To share sessionState on any website you need to set the same sessionId on them. You need to pass sessionId from source website to destination either using GET of POST methods. The codes below do it for you by using GET method (using Request.QueryString) : 

using System.Web.SessionState;
protected void Page_Load(object sender, EventArgs e)
    if (Session["UserId"] == null) // Is Shared SessionState Available?
        string sessionId = Request.QueryString["sid"];
        // The SessionId From Source Website.

        SessionIDManager manager = new SessionIDManager();
        bool isRedirected; // Get true if cookieless=true and you redirected to the current page.
        bool isNewSession; // Get true if a new "ASP.NET_SessionId" cookie will be created.
        manager.SaveSessionID(Context, sessionId, out isRedirected, out isNewSession);
        // Change sessionId in httpResponse of the current Context

        Session["Temp"] = "Some useless data.";
        // this operation is neccesary because sessionId read from Request and write to Response

        Response.Redirect(Request.Path, true);
        // Now redirect to current page to check if shared SessionState is available
        // Do something after sharing the sessionStates


  • Version 1.0: Share session states in sub domains of current domain.
  • Version 1.1: Share session states in different domains.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Hossein Shahpouri
Web Developer
Iran (Islamic Republic Of) Iran (Islamic Republic Of)
No Biography provided

You may also be interested in...

Comments and Discussions

Questiongreat hack! Pin
di~v~inci7-Feb-15 12:30
memberdi~v~inci7-Feb-15 12:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150901.1 | Last Updated 22 Apr 2013
Article Copyright 2013 by Hossein Shahpouri
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid