Sharing session between apps with SqlServer in Asp.NET

I spent about 3 hours figuring out how to get two separate applications to be able to share their session variables.  The answer, once all said and done, was much simpler than expected.

First, an ASSUMPTION I make is that one app is the child of another app in IIS

The two keys:

  • [code language=”xml”]<sessionState mode="SQLServer" sqlConnectionString="Integrated Security=SSPI;data source=YOUR_DB_INSTANCE;Application Name=YOUR_APP_NAME_IDENTICAL_IN_APPS_SHARING_SESSION" cookieless="AutoDetect"/>[/code]
  • Then, run this Stored Procedure.  It looks at the Application Name in your connection string, and when your apps have identical names here, it will assign them the same id – therefore giving them access to the same session on SQL.

That’s it.  Done.  All that effort for two changes – although I wasn’t going to figure out the SP trick just by staring at it – I modified the web.configs multiple times before realizing it all hinges on the SP vs the web.config settings.

*I’d imagine this will work even if your apps don’t meet this criteria, since all the action is happening on the DB side, but I’m just clarifying the setup I’m using