Click here to Skip to main content
13,866,049 members
Click here to Skip to main content
Add your own
alternative version


63 bookmarked
Posted 18 Jun 2008
Licenced CPOL

Sharing Session Across Applications

, 18 Jun 2008
Rate this:
Please Sign up or sign in to vote.
A simple way to share session data accross web applications.


This is intended to be a simple solution to sharing session data across applications. This implementation allows you to use the out of the box session providers that ship with ASP.NET without modifying any stored procedures, or creating custom session store providers.


There was a large project going on at work, part of which involved splitting our one large website into smaller logical areas of functionality. This would provide us with more flexibility and manageability of our products. The only problem was, how would we share session data across applications?

Our current environment uses a SQL session store, so my first suggestion was to simply modify one of the stored procs to give all applications the same name, thus sharing session. That was quickly shot down by coworkers because they felt it would cause a support hole in the case that Microsoft would have to come in to troubleshoot anything related to session data. So, they decided to create custom session store providers. What they ended up doing is essentially creating exact code duplicates of the MS implementations for SQL and State Server session stores, and hacking in a few lines to change the application name (I am not sure how that is any better that changing a stored proc, but that was the decision).

As time came to actually implement this in our applications, I looked over the proof of concept, and found myself unable to actually use all that copied code simply to share session data. I just couldn't believe that there wasn't an easier way to solve this problem.

After spending about an hour searching the Internet, and not finding anything better, I decided to dig into the session manager code using Reflector to see if I could find anything.

Here's what I came up with...

Using the code

This shared session solution I came up with is fairly simple to use. Simply plug in the SharedSessionModule...

In the web.config of each application you would like to share session data for, add the following HTTP module:

   <add name="SharedSessionModule" type="Femiani.Web.Modules.SharedSessionModule, 

Then, set your application name:

    <add key="ApplicationName" value="SharedWeb"/>

Points of interest

After digging around for a while, I found out where the session stores were getting the application name from. Then, using an HTTP module and some Reflection, I manually set the application name when the web application starts up.

/// <span class="code-SummaryComment"><summary></span>
/// SharedSessionModule class.
/// <span class="code-SummaryComment"></summary></span>
/// <span class="code-SummaryComment"><created date="5/31/2008" by="Peter Femiani"/></span>
public class SharedSessionModule : IHttpModule
    #region IHttpModule Members
    /// <span class="code-SummaryComment"><summary></span>
    /// Initializes a module and prepares it to handle requests.
    /// <span class="code-SummaryComment"></summary></span>
    /// <span class="code-SummaryComment"><param name="context">An <see cref="T:System.Web.HttpApplication"/></span>
    /// that provides access to the methods,
    /// properties, and events common to all application objects within an ASP.NET
    /// application<span class="code-SummaryComment"></param></span>
    /// <span class="code-SummaryComment"><created date="5/31/2008" by="Peter Femiani"/></span>
    public void Init(HttpApplication context)
            // Get the app name from config file...
            string appName = ConfigurationManager.AppSettings["ApplicationName"];
            if (!string.IsNullOrEmpty(appName))
                FieldInfo runtimeInfo = typeof(HttpRuntime).GetField("_theRuntime", 
                                        BindingFlags.Static | BindingFlags.NonPublic);
                HttpRuntime theRuntime = (HttpRuntime)runtimeInfo.GetValue(null);
                FieldInfo appNameInfo = typeof(HttpRuntime).GetField("_appDomainAppId", 
                                        BindingFlags.Instance | BindingFlags.NonPublic);
                appNameInfo.SetValue(theRuntime, appName);
        catch (Exception ex)
    /// <span class="code-SummaryComment"><summary></span>
    /// Disposes of the resources (other than memory) used by the module that
    /// implements <span class="code-SummaryComment"><see cref="T:System.Web.IHttpModule"/>.</span>
    /// <span class="code-SummaryComment"></summary></span>
    /// <span class="code-SummaryComment"><created date="5/31/2008" by="Peter Femiani"/></span>
    public void Dispose()

And that's it! Pretty simple.


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


About the Author

Architect People Media
United States United States
My name is Peter Femiani and I am a graduate of Arizona State University with a B.S. in Economics. I have been writing code since I was about 14.

You may also be interested in...


Comments and Discussions

Generalimpressively easy :-) Pin
cribe8-Oct-08 9:52
membercribe8-Oct-08 9:52 
GeneralNo Funciona Pin
Joshue24-Sep-08 8:46
memberJoshue24-Sep-08 8:46 
GeneralNeed urgent help Pin
mamtamastek24-Sep-08 4:33
membermamtamastek24-Sep-08 4:33 
GeneralSharing session between .net 1.1 and 2.0 Pin
wijayae27-Aug-08 0:20
memberwijayae27-Aug-08 0:20 
Generalsharing session data accross servers Pin
Desayya N21-Aug-08 20:59
memberDesayya N21-Aug-08 20:59 
GeneralInProc and StateServer Modes Pin
Pavi Sivya20-Aug-08 7:19
memberPavi Sivya20-Aug-08 7:19 
GeneralRe: InProc and StateServer Modes Pin
issac@limosys.com22-Jan-10 7:45
memberissac@limosys.com22-Jan-10 7:45 
GeneralSome issues [modified] Pin
mrcraft114-Aug-08 6:45
membermrcraft114-Aug-08 6:45 
Info about my machine:

- IIS 5 Win XP Pro
- ASP.NET 2.0
- Two (2.0) applications running, one called "Web" the other called "Test". Web sets a value in session and Test tries to retrieve it.
- Using ASP.NET State Server

AppDomainId values:

"Web" = /LM/w3svc/1/ROOT/Web
"Test" = /LM/w3svc/1/ROOT/Test

1) In your example you set the ApplicationName in the config to just the name of the virtual directory. Isn't this incorrect? Shouldn't the value be something like "/LM/W3SVC/1/Root/VirtualAppName"?

2) I am unable to get it to work between applications. I have tried specifying the _appDomainAppId value that i get from the one application, "/LM/w3svc/1/ROOT/Web", in the config for the other and it didn't work. The sessionId cookie is being passed in and it's the same value for both, but session state has no keys and no data. I then tried doing as you did in your example with just the Virtual directory name, in thise case, "Web", and that didn't work either. I then tried specifying the machine key value in the config (as needs to be done in a web farm environment) and it also did not work.

I verified that application "Web" was actually adding the values to session successfully by counting the Keys and also checking the value. I do the same in Test but always get 0 keys and no data. I then also compared the _appDomainAppId value from the HttpRuntime and saw the appDomainId values are identical so it is definitely setting the value correctly.

Any ideas? I have searched on other ways to share data across applications and found only the T-SQL hack for SqlServer session and people implementing their own custom session state store.

modified on Thursday, August 14, 2008 12:01 PM

GeneralRe: Some issues Pin
frankbret210-Dec-08 11:11
memberfrankbret210-Dec-08 11:11 
GeneralNot working for ASP State Server Pin
EdwinY11-Aug-08 21:32
memberEdwinY11-Aug-08 21:32 
GeneralRe: Not working for ASP State Server Pin
pfemiani12-Aug-08 6:06
memberpfemiani12-Aug-08 6:06 
GeneralRe: Not working for ASP State Server Pin
SashiRachakonda29-Feb-12 15:14
memberSashiRachakonda29-Feb-12 15:14 
GeneralI have an error in my web.config [modified] Pin
Member 24283424-Aug-08 20:48
memberMember 24283424-Aug-08 20:48 
AnswerRe: I have an error in my web.config Pin
Dutchboy00343-Jan-10 4:02
memberDutchboy00343-Jan-10 4:02 
QuestionDoes this work in 2.0 and vb Pin
Asha100917-Jul-08 11:33
memberAsha100917-Jul-08 11:33 
Question.NET 3.5 only? Pin
SIMONJONES10-Jul-08 7:32
memberSIMONJONES10-Jul-08 7:32 
AnswerRe: .NET 3.5 only? Pin
pfemiani16-Jul-08 9:10
memberpfemiani16-Jul-08 9:10 
GeneralRe: .NET 3.5 only? Pin
sagitario348-Aug-08 5:20
membersagitario348-Aug-08 5:20 
GeneralRe: .NET 3.5 only? Pin
EdwinY5-Apr-09 22:57
memberEdwinY5-Apr-09 22:57 
AnswerRe: .NET 3.5 only? Pin
EdwinY11-Aug-08 21:13
memberEdwinY11-Aug-08 21:13 
GeneralRe: .NET 3.5 only? Pin
Suresh SD10-Sep-08 4:02
memberSuresh SD10-Sep-08 4:02 
GeneralQuestions Pin
Paulo Morgado24-Jun-08 6:54
professionalPaulo Morgado24-Jun-08 6:54 
GeneralRe: Questions Pin
pfemiani24-Jun-08 7:15
memberpfemiani24-Jun-08 7:15 
GeneralRe: Questions Pin
Paulo Morgado24-Jun-08 12:55
professionalPaulo Morgado24-Jun-08 12:55 
GeneralFile Corrupted Pin
Sergio Luix19-Jun-08 8:35
memberSergio Luix19-Jun-08 8:35 

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

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

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web02 | 2.8.190214.1 | Last Updated 19 Jun 2008
Article Copyright 2008 by pfemiani
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid