title: ASP.NET Calling WebMethod with jQuery AJAX "401 (Unauthorized)"
body: <p>Been stuck with this for hours</p>
  
  
  
  <pre><code>{"Message":"Authentication failed.","StackTrace":null,"ExceptionType":"System.InvalidOperationException"}
  </code></pre>
  
  <p>I'm trying to call this WebMethod in my ASP.Net Webform
  </p>
  
  <pre><code>[WebMethod]
  public static string GetClients(string searchTerm, int pageIndex)
  {
      string query = "[GetClients_Pager]";
      SqlCommand cmd = new SqlCommand(query);
      cmd.CommandType = CommandType.StoredProcedure;
      cmd.Parameters.AddWithValue("@SearchTerm", searchTerm);
      cmd.Parameters.AddWithValue("@PageIndex", pageIndex);
      cmd.Parameters.AddWithValue("@PageSize", PageSize);
      cmd.Parameters.Add("@RecordCount", SqlDbType.Int, 4).Direction = ParameterDirection.Output;
      return GetData(cmd, pageIndex).GetXml();
  }
  </code></pre>
  
  <p>From this jquery.ajax
  </p>
  
  <pre><code>function GetClients(pageIndex) {
      $.ajax({
          type: "POST",
          url: "ConsultaPedidos.aspx/GetClients",
          data: '{searchTerm: "' + SearchTerm() + '", pageIndex: ' + pageIndex + '}',
          contentType: "application/json; charset=utf-8",
          dataType: "json",
          success: OnSuccess,
          failure: function (response) {
              alert(response.d);
              },
              error: function (response) {
                  alert(response.d);
              }
      });
  }
  </code></pre>
  
  <p>But I always get this error:</p>
  
  <blockquote>
    <p>POST <code>http://localhost:64365/ConsultaPedidos.aspx/GetClients</code> <strong>401
    (Unauthorized)</strong></p>
  </blockquote>
  
  <p>Weird thing is that this used to work until I start authenticating users</p>
  
  
  
  <pre><code>&lt;system.web&gt;
  ...
      &lt;authentication mode="Forms"&gt;
        &lt;forms loginUrl="~/Account/Login" timeout="2880" defaultUrl="/Dashboard" /&gt;
      &lt;/authentication&gt;
      &lt;authorization&gt;
        &lt;deny users="?" /&gt;
      &lt;/authorization&gt;
  ...
  &lt;/system.web&gt;
  </code></pre>
  
  <p>Any ideas?</p>