title: Why can't I share Session state between 2 web apps with StateServer? What am I missing?
body: <p>I'm having trouble getting 2 identical ASP.NET MVC applications to share the same Session using a Session StateServer. The reason I'm trying to do this is we will eventually be deploying this app across 3 web servers that need to share the same state. We need to use StateServer because we are trying to minimise use of the db for non data-related storage.</p>
  
  <p><strong>The Setup:</strong> </p>
  
  <p>I've deployed the same code base to <a href="http://localhost/" rel="noreferrer">http://localhost/</a><strong>App1</strong> and <a href="http://localhost/" rel="noreferrer">http://localhost/</a><strong>App2</strong></p>
  
  <p>both have identical Web.Config files with the following:</p>
  
  <pre><code>&lt;system.web&gt;
  &lt;sessionState mode="StateServer" 
                cookieless="false" 
                timeout="20" 
                stateConnectionString="tcpip=127.0.0.1:42424" /&gt;
                //stateConnectionString="tcpip=192.168.1.52:42424" /&gt; // also doesn't work
  &lt;machineKey 
    validationKey="8B9F68D0CC730F6F046D0173021C34B1A0D9A01C21D8E4D4A7A1DFF38332DEE8CBBAFEA503C18776614EE9D4F7EEA7E5D2D5571630547D822485A27B1EF53AC1"
    decryptionKey="60009563EFCFC594FD1BC46684943AA398EE70412A624B2EB488BBB071F15ECF"
    validation="SHA1" decryption="AES" /&gt;
  </code></pre>
  
  <p>I used <a href="http://www.developmentnow.com/articles/machinekey_generator.aspx" rel="noreferrer">this tool to generate these machine keys</a></p>
  
  <p><strong>The Test:</strong></p>
  
  <p>I put the following into one of my Controllers to test if it was working:</p>
  
  <pre><code>ViewData["mode"] = requestContext.HttpContext.Session.Mode.ToString();
  
  string timestamp = DateTime.Now.ToString();
  if (requestContext.HttpContext.Session["timestamp"] == null)
  {
      requestContext.HttpContext.Session["timestamp"] = timestamp;
  }
  
  ViewData["timestamp"] = requestContext.HttpContext.Session["timestamp"].ToString();
  ViewData["realtime"] = timestamp;
  </code></pre>
  
  <p>with this in the view:</p>
  
  <pre><code>&lt;p&gt;
      Mode: &lt;%= ViewData["mode"].ToString() %&gt;
  &lt;/p&gt;
  &lt;p&gt;
      Time: &lt;%= ViewData["timestamp"].ToString() %&gt;
  &lt;/p&gt;
  &lt;p&gt;
      real time: &lt;%= ViewData["realtime"].ToString() %&gt;
  &lt;/p&gt;
  </code></pre>
  
  <p><strong>The Result:</strong></p>
  
  <p>For both deployments, when the page first loads I can see that the mode is StateServer and the timestamp is getting set to the same time as the realtime value.. However, if this was working, only the first page should have the same time as the realtime value. The second page load should read from the StateServer because that timestamp value is no longer null, and display that time value. But instead, it's displaying the realtime value again.</p>
  
  <p>When I refresh the page, I the timestamp stays the same and the realtime value is always updating. This indicates that the timestamp is being saved to the Session, but the time stamp value is always different for both deployments when it should be the same, so this indicates that the Session is not being shared.</p>
  
  <p>Can somebody point out if I'm doing something wrong or if there's something else I need to do to get this to work? Thanks</p>