Sharing Session State using state server RRS feed

  • Question

  • User-1028964456 posted

    I have two subdomains that I want to share session state.  They both point to the same state server.  I have used sqlstate in the past but it is too slow, so we needed to use stateserver.  My understanding is that

    1.  I need the session ids to share the same domain name

    2. The application ids need to be the same. 

    I have been able to get the session IDs to be shared, but, I can't get the application ID to be the same.

    I tried an httpModule with code (https://support.microsoft.com/en-ae/help/2527105/how-to-share-session-state-across-subdomains) but it doesn't seem to do anything


    public class SharedSessionModule:IHttpModule
    protected static string applicationName = ConfigurationManager.AppSettings["ApplicationName"];
    protected static string rootDomain = ConfigurationManager.AppSettings["RootDomain"];

    public void Dispose()
    throw new NotImplementedException();

    public void Init(HttpApplication context)
    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, applicationName);

    context.PostRequestHandlerExecute += new EventHandler(context_PostRequestHandlerExecute);

    void context_PostRequestHandlerExecute(object sender, EventArgs e)
    HttpApplication context = (HttpApplication)sender;
    HttpCookie cookie = context.Response.Cookies["ASP.NET_SessionId"];

    if (context.Session != null &&
    cookie.Value = context.Session.SessionID;
    if (rootDomain != "localhost")
    cookie.Domain = rootDomain;
    cookie.Path = "/";



    I am looking at System.Web.Hosting.HostingEnvironment.ApplicationHost.GetSiteName() to confirm if the application ID has changed?  

    Is this the correct object to look at to confirm the module is working?

    How can I tell if the module is even running and / or other suggestions?

    Any ideas?  Am I barking up the correct tree here?


    Tuesday, January 28, 2020 7:19 PM

All replies

  • User2053451246 posted

    The Application Name should be defined in the Web.config file, and that is used to lookup the Application Id.  Make the Application Name the same in both Web.config files.

    <add name="SqlRoleManager" type="System.Web.Security.SqlRoleProvider" connectionStringName="ApplicationServicesContext" applicationName="/YourAppName" />

    Tuesday, January 28, 2020 9:07 PM
  • User-1028964456 posted

    I'm not using the built in role management capabilities for these sites.  Does sqlrolemanager still apply?

    Tuesday, January 28, 2020 10:05 PM
  • User2053451246 posted

    I used the Role Manager entry as an example of how an Application Name is used in the .config file to do a database lookup to get the Application Id.  Does state server have a similar entry defined in the .config file that has an applicationName attribute like my example?

    Does the value for ApplicationName (which must be defined in your .config file) lookup something in a DB to get the Application Id?

    protected static string applicationName = ConfigurationManager.AppSettings["ApplicationName"];

    Seeing the above line means you have should have something called ApplicationName defined in the <appSettings> section of a config file.

    Thursday, January 30, 2020 4:36 AM