title: Cannot get Session state in Web Service from ajax on client
body: <p>I would like to have only 1 session for each user. But the system generate new one session when I run ajax query.
  There were no problems when I tested it on debug mode on my computer. Problems rised only in  client site on distributed application.</p>
  
  <p>ASP page:</p>
  
  <pre><code>&lt;asp:TextBox ID="TextBox1" runat="server"&gt;&lt;/asp:TextBox&gt;
  &lt;input id="Text1" type="text" /&gt;&lt;br /&gt;
  &lt;asp:Button ID="btn_ASP" runat="server" onclick="btn_ASP_Click" Text="Asp" /&gt;
  &lt;input id="btn_JS" type="button" value="JS" onclick="GetUsersList();" /&gt;&lt;br /&gt;
  </code></pre>
  
  <p>Asp button which generates 'ConnectCount' Session element:</p>
  
  <pre><code>protected void btn_ASP_Click(object sender, EventArgs e)
  {
      int count;
      if (HttpContext.Current.Session["ConnectCount"] == null)
          count = 1;
      else
          count = (int)HttpContext.Current.Session["ConnectCount"] + 1;
      HttpContext.Current.Session["ConnectCount"] = count;
      TextBox1.Text = count.ToString();          
  }
  </code></pre>
  
  <p>Web Service code:</p>
  
  <pre><code>[WebMethod(EnableSession = true)]
  [ScriptMethod]
  public string Get_UsersList2()
  {
      int count;
      if (HttpContext.Current.Session["ConnectCount"] == null)
          count = 1;
      else
          count = (int)HttpContext.Current.Session["ConnectCount"] + 1;
      HttpContext.Current.Session["ConnectCount"] = count;
      return count.ToString();
  }
  </code></pre>
  
  <p>Ajax query to Web Service</p>
  
  <pre><code>function GetUsersList() {            
      $.ajax({
          url: '/WbSrv_ACTR.asmx/Get_UsersList2',
          type: "POST",
          contentType: "application/json; charset=utf-8",
          data: '{}',
          success: function (data, st) {
              if (st == "success") {
                  if (data.d != "") {
                      text1.value = data.d;
                  }
                  else {
                  }
              }
          },
          error: function () {
              alert("Cannot get list of users");
          }
      });
  }
  </code></pre>
  
  <p>Code in buttons just adds 1 to Session["ConnectCount"].</p>
  
  <p>So when I click 5 times on Asp button and 1 time on JQ button (on local IIS - testing):</p>
  
  <pre><code>result: asp = 5, jq = 6 (correct)
  </code></pre>
  
  <p>do the same operations on server IIS 7.5:</p>
  
  <pre><code>result: asp = 5, jq = 1 (incorrect)
  </code></pre>
  
  <p>Server uses other session.
  For testing - I've added some elements in Session via ASP button and couldn't find them in Web Service (Session was empty there but <strong>SessionID was the same</strong>)</p>
  
  <p><strong>Could anyone have solution or workaround?</strong></p>
  
  <p>Web config:</p>
  
  <pre><code>&lt;system.web&gt;
    &lt;compilation debug="true" targetFramework="4.0"&gt;
      &lt;assemblies&gt;
        &lt;add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" /&gt;
      &lt;/assemblies&gt;
    &lt;/compilation&gt;
    &lt;customErrors mode="Off" /&gt;
    &lt;authorization&gt;
        &lt;deny users="?" /&gt;
    &lt;/authorization&gt;
  &lt;/system.web&gt;
  &lt;system.webServer&gt;
      &lt;security&gt;
          &lt;authentication&gt;
              &lt;windowsAuthentication enabled="true" /&gt;
              &lt;anonymousAuthentication enabled="false" /&gt;
          &lt;/authentication&gt;
      &lt;/security&gt;
  &lt;/system.webServer&gt;
  </code></pre>