title: Sharing SQL Server Session State across Web Applications
body: <p>I'm setting up a very basic demo of SQL Server Session State, but am having some trouble getting it working. I'm trying to test this out locally running Windows 7 with IIS 7.5 and SQL Server 2008 R2. </p>
  
  <p><strong>Ultimately, I need a way to track the number of users logged into a system that is load-balanced between a few different web servers. So I need to update a session variable (stored in SQL) every time a user logs in or out. So the session ID could always be different. Is this possible?</strong></p>
  
  <p>Here's what I've done so far:</p>
  
  <ol>
  <li>Created two new sites in IIS (Site1 and Site2)</li>
  <li>Created two new Web Application projects in VS 2010 (Site1 and Site2)</li>
  <li>In both sites, on the Default.aspx page, I created a button that when clicked will save either "Site1" or "Site2" (depending on the site) into a session variable called "LastSiteUsed". There's another button, that when clicked, will read the value from the "LastSiteUsed" session variable and display it in a label.</li>
  <li>In SQL, I created a new Database called dbSessionTest. Then I followed the instructions on <a href="http://msdn.microsoft.com/en-us/library/ms178586.aspx" rel="nofollow noreferrer">MSDN to install the Session State Database using the aspnet_regsql tool</a>.</li>
  <li>In both of my Web Application's Web.config file, I've added the following under <code>&lt;System.Web&gt;</code>: </li>
  </ol>
  
  <p><code>&lt;sessionState mode="SQLServer" sqlConnectionString="server=.\sqlexpress;database=dbSessionTest;uid=myUsername;pwd=myPassword" cookieless="false" timeout="20" allowCustomSqlDatabase="true"/&gt;</code></p>
  
  <p>Both sites function normally, but they don't seem to be <em>sharing</em> the session. For example, when I click my button to save my session variable from Site1, I'd expect to be able to read that value from Site2, but it doesn't work. Site2 will only return "Site2" and Site1 will only return "Site1". </p>
  
  <p>Does anyone have any ideas on what I could be doing wrong? Am I wrong in thinking that I should be able to read a value set by Site1 from Site2?</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>I can see that session data is getting stored in the <code>ASPStateTempSessions</code> table in SQL Management Studio, but each site can still only see the value which it wrote. Both sites are setting the session variable like this:</p>
  
  <pre><code>Session("LastSiteUsed") = "Site2"
  </code></pre>
  
  <p>And both sites are retrieving the value like:</p>
  
  <pre><code>lblValue.Text = "Value from Session: " &amp; Session("LastSiteUsed").ToString
  </code></pre>
  
  <p>Do I need to do this differently for accessing session variables stored in SQL Server?</p>
  
  <p><strong>UPDATE 2:</strong></p>
  
  <p>I've tried using the default database, ASPState, which is created by running this command:</p>
  
  <pre><code>aspnet_regsql.exe -S MyServerName -E -ssadd -sstype p
  </code></pre>
  
  <p>Then simplified each of my Web.config files like:</p>
  
  <pre><code>&lt;sessionState mode="SQLServer" sqlConnectionString="Data Source=.\sqlexpress;User ID=myUsername;Password=myPassword" cookieless="false" timeout="20"/&gt;
  </code></pre>
  
  <p>But again, no luck. I want to be able to set a session variable from Site1 and then read the value from Site2, but it's not working. Again, I'm able to see entries showing up in the <code>ASPStateTempSessions</code> table so I know they're getting entered correctly. One thing I noticed is they have different session IDs? </p>
  
  <p>Is there something I need to be doing differently to ensure the same session ID is used between my sites when setting/reading the same session variable?</p>
  
  <p><strong>UPDATE 3:</strong></p>
  
  <p>I've followed the instructions from <strong><a href="http://www.stionasoftware.com/Blogs/post/2010/10/27/Sharing-NET-Session-across-multiple-applications.aspx" rel="nofollow noreferrer">this article</a></strong> which modifies an SP and adds a column for grouping to the <code>ASPStateTempApplications</code> table. This <em>kind of</em> worked, but only if both of my sites were open in the same browser (using tabs). I need to be able to open Site1 in IE, save a value to my session variable, close the browser, open Site2 in Chrome, and read the value. From everything I've read with SQL Server Session State... this should be possible.</p>
  
  <p><strong>UPDATE 4 - SOLUTION:</strong></p>
  
  <p>I followed the answer on <strong><a href="https://stackoverflow.com/questions/2868316/sharing-sessions-across-applications-using-the-asp-net-session-state-service/3151315#3151315">this article</a></strong> provided by @SilverNinja. I re-created the <code>ASPState</code> database via the command prompt (to undo my SP changes in Update #3) and then modified the <code>TempGetAppID</code> SP as per the answer from the link. I've also updated both of my Web.config files to match the answer from that article as well:</p>
  
  <pre><code>&lt;sessionState mode="SQLServer"
                sqlConnectionString="Data Source=.\sqlexpress;User ID=myUsername;Password=myPassword;Application Name=CacheTest"/&gt;
  </code></pre>
  
  <p>I've also included identical Machine Keys in both Web.config files:</p>
  
  <pre><code>&lt;machineKey validationKey="59C42C5AB0988049AB0555E3F2128061AE9B75E3A522F25B34A21A46B51F188A5C0C1C74F918DFB44C33406B6E874A54167DFA06AC3BE1C3FEE8506E710015DB" decryptionKey="3C98C7E33D6BC8A8C4B7D966F42F2818D33AAB84A81C594AF8F4A533ADB23B97" validation="SHA1" decryption="AES" /&gt;
  </code></pre>
  
  <p>Now I am able to (using IE) open up both of my sites, set a value from Site1, and read it from Site2 (and vice versa). When I check the table, only one SessionID exists (both sites are using the same session correctly). I was wrong in my thinking before that opening a new browser (for example, with Chrome), would use the same session - a new browser will start it's own session. On a load-balanced scenario though, this shouldn't cause any issues.</p>