Sharing session between two web applications

05 Mar 2018 Kamal Pratap
4 Comments 9593 Views

Sometime we need to share sessions between two application. For example we have two application have same information but language is different then how to switch the website language whether member is login.


Here we will explain how to solve the problem to share session between Web application.

I have two exactly the same web sites in different language hosted under IIS. ASP.NET state service is running on my machine and the web.config is updated in both sites with the following code:

Step 1: Make sure ASP .NET state service is running. Start the state service following the given steps
             Open a run box. 
             Type services.msc and press Ok.
              Start Asp.Net State Service

Step 2 : Application name must be matched. Add the key under App Setting in Web.Config on both website.

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

Step 3 : Add the machineKey in Web.Config on both website.

<machineKey validationKey="7CB8DF6872FB6B35DECD3A8F55582350FEE1FAB9BE6B930216056C1B5BA69A4C5777B3125A42C4AECB4419D43EC12F168FD1BB887469798093C3CAA2427B2B89"
    validation="SHA1" decryption="AES"/>

Step 4 : Add sessionState in Web.Config on both website.

<sessionState cookieless="UseCookies" mode="StateServer" regenerateExpiredSessionId="false" timeout="120">
      <clear />

Step 5 : Add below code in Application setting file (Global.asax)

 public override void Init()
                // Get the app name from config file...
                string appName = ConfigurationManager.AppSettings["ApplicationName"];
                if (!string.IsNullOrEmpty(appName))
                    foreach (string moduleName in this.Modules)
                        IHttpModule module = this.Modules[moduleName];
                        SessionStateModule ssm = module as SessionStateModule;
                        if (ssm != null)
                            FieldInfo storeInfo = typeof(SessionStateModule).GetField("_store", BindingFlags.Instance | BindingFlags.NonPublic);
                            SessionStateStoreProviderBase store = (SessionStateStoreProviderBase)storeInfo.GetValue(ssm);
                            if (store == null) //In IIS7 Integrated mode, module.Init() is called later
                                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);
                                Type storeType = store.GetType();
                                if (storeType.Name.Equals("OutOfProcSessionStateStore"))
                                    FieldInfo uribaseInfo = storeType.GetField("s_uribase", BindingFlags.Static | BindingFlags.NonPublic);
                                    uribaseInfo.SetValue(storeType, appName);

            catch (Exception ex)
                string s = ex.Message.ToString();

After doing the above steps now you are able to share sessions between Asp.Net applications.

Kamal Pratap

I have 6+ years experience in .Net technologies like Asp.Net, C#, WCF, Web Services, SQL Server, Ajax, LinQ. Currently I am working in Netcarrots Loyalty Services as a Software Developer.


  1. This worked with IIS Express but didn't when deployed on IIS as separate websites. Could you try yourself ?

  2. Sir i used above code but still unable to share session across my two domains i did exactly what u said but i dont know what i am missing ?sir please help me

Leave a Comment