Click here to Skip to main content
11,490,282 members (55,270 online)
Click here to Skip to main content

Sharing Session Across Applications

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

Introduction

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.

Background

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:

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

Then, set your application name:

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

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)
    {
        try
        {
            // 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)
        {
            Debug.WriteLine(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()
    {
    }
    #endregion
}

And that's it! Pretty simple.

License

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

Share

About the Author

pfemiani
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.

Comments and Discussions

 
GeneralMy vote of 1 Pin
Member 92665974-Sep-14 1:59
memberMember 92665974-Sep-14 1:59 
AnswerAccessing shared session in Multiple MVC Web Applcations, Environment MVC 4, IIS7 Pin
Sandeep Negi30-Jun-14 0:21
memberSandeep Negi30-Jun-14 0:21 
Questionme too except Pin
me_pollack28-Mar-14 11:44
memberme_pollack28-Mar-14 11:44 
GeneralMy vote of 1 Pin
Pratik Bhuva18-Feb-14 3:40
professionalPratik Bhuva18-Feb-14 3:40 
QuestionShare session Pin
cnnbs13-Feb-14 5:30
membercnnbs13-Feb-14 5:30 
GeneralMy vote of 1 Pin
abdussalam14331-Jan-14 3:59
professionalabdussalam14331-Jan-14 3:59 
GeneralMy vote of 1 Pin
Mannava Siva Aditya2-May-13 21:23
memberMannava Siva Aditya2-May-13 21:23 
QuestionSession is clear - No session data found Pin
Mannava Siva Aditya2-May-13 21:06
memberMannava Siva Aditya2-May-13 21:06 
QuestionNo session data found. Pin
ashishishwar20-Mar-13 3:39
memberashishishwar20-Mar-13 3:39 
QuestionI got it to work with FW 3.5 (SharePoint 2010) with a minor change Pin
emadm5-Oct-12 7:08
memberemadm5-Oct-12 7:08 
Questioniis 7.5 Pin
Member 78957013-Jul-12 5:10
memberMember 78957013-Jul-12 5:10 
QuestionSession not working in Second Application Pin
manojkumar.m.sierra22-Feb-12 20:39
membermanojkumar.m.sierra22-Feb-12 20:39 
BugBugfixes Pin
Sergiy Sakharov9-Feb-12 7:37
memberSergiy Sakharov9-Feb-12 7:37 
GeneralRe: Bugfixes Pin
level3comm1-Mar-12 8:39
memberlevel3comm1-Mar-12 8:39 
GeneralRe: Bugfixes Pin
Member 859114921-Jun-12 3:06
memberMember 859114921-Jun-12 3:06 
GeneralRe: Bugfixes Pin
Member 78957011-Jul-12 23:59
memberMember 78957011-Jul-12 23:59 
QuestionRe: Bugfixes Pin
Sunil12331-Jul-13 17:51
memberSunil12331-Jul-13 17:51 
QuestionCan't access session variable in second web application Pin
Member 393598417-Aug-11 21:17
memberMember 393598417-Aug-11 21:17 
GeneralFailed Pin
zohreh_av13-Apr-11 21:40
memberzohreh_av13-Apr-11 21:40 
GeneralWorking Except Redeployments require me to recycle the app pool Pin
test89214-Jan-11 11:52
membertest89214-Jan-11 11:52 
GeneralThis is not working Pin
aesalan11-Oct-10 4:10
memberaesalan11-Oct-10 4:10 
GeneralThis is not working Pin
Member 945980925-Sep-12 20:30
memberMember 945980925-Sep-12 20:30 
GeneralNot able to retreive the session in the second web application Pin
tarungogia4-Oct-10 4:33
membertarungogia4-Oct-10 4:33 
QuestionIn IIS7 Pin
Rajan Devaraj2-Jun-10 8:25
memberRajan Devaraj2-Jun-10 8:25 
AnswerRe: In IIS7 Pin
avalon12311-Aug-11 5:49
memberavalon12311-Aug-11 5:49 
GeneralRe: In IIS7 Pin
Member 78957012-Jul-12 0:07
memberMember 78957012-Jul-12 0:07 
GeneralNew approach - with Distributed Cache Pin
Krenning16-Jan-10 23:53
memberKrenning16-Jan-10 23:53 
AnswerProblem Solved - IIS 7 modification..... Pin
Krenning3-Jan-10 4:19
memberKrenning3-Jan-10 4:19 
GeneralRe: Problem Solved - IIS 7 modification..... Pin
karotijam23-Feb-10 7:04
memberkarotijam23-Feb-10 7:04 
GeneralNot working for me Pin
mbowles20117-Sep-09 4:16
membermbowles20117-Sep-09 4:16 
AnswerRe: Not working for me Pin
Krenning3-Jan-10 4:00
memberKrenning3-Jan-10 4:00 
GeneralMy vote of 1 Pin
Santosh G17-May-09 22:06
memberSantosh G17-May-09 22:06 
GeneralRe: My vote of 1 Pin
Krenning3-Jan-10 3:54
memberKrenning3-Jan-10 3:54 
GeneralElegant solution but doesn't work for me Pin
Sphengle23-Apr-09 8:13
memberSphengle23-Apr-09 8:13 
GeneralThanks Pin
shahzadsb14-Apr-09 12:04
membershahzadsb14-Apr-09 12:04 
GeneralAlways new session accross domains Pin
hcakir14-Mar-09 16:04
memberhcakir14-Mar-09 16:04 
GeneralNot working with Response.Redirect Pin
xueco23-Jan-09 7:40
memberxueco23-Jan-09 7:40 
GeneralRe: Not working with Response.Redirect Pin
xueco26-Jan-09 1:08
memberxueco26-Jan-09 1:08 
QuestionWhat environment did you use? It did not work IIS 7.0, VS 2008 Pin
details@insidespirit.com1-Jan-09 16:43
memberdetails@insidespirit.com1-Jan-09 16:43 
QuestionDidn't work Pin
Claddha16-Dec-08 6:01
memberCladdha16-Dec-08 6:01 
AnswerRe: Didn't work Pin
Cagdas Basaraner15-Jun-10 21:37
memberCagdas Basaraner15-Jun-10 21:37 
GeneralAsp.Net 1.1 Pin
Shayis30-Nov-08 23:22
memberShayis30-Nov-08 23:22 
GeneralConfiguration Error [modified] Pin
-rain-27-Nov-08 0:07
member-rain-27-Nov-08 0:07 
Questionsharing session data accross servers? Pin
Valens_Lu19-Nov-08 19:35
memberValens_Lu19-Nov-08 19:35 
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
Member 277935621-Aug-08 20:59
memberMember 277935621-Aug-08 20:59 
GeneralInProc and StateServer Modes Pin
Pavi Sivya20-Aug-08 7:19
memberPavi Sivya20-Aug-08 7:19 

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
Web03 | 2.8.150520.1 | Last Updated 19 Jun 2008
Article Copyright 2008 by pfemiani
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid