title: Call web api with basic authentication always get 401 unauthorized from IIS
body: <p>Tonight i come search some help about how to call a web api hosted in IIS.</p>
  
  <p><strong><em>Everything work well</em></strong> in local from visual studio to iis express. But strangely, after publish on my IIS server. I always get 401 unauthorized  :'(</p>
  
  <p>Here is the code i use and the settings from my IIS server. I will be very grateful if somebody can help me.
  Thank you</p>
  
  <p>**</p>
  
  <h2>The controller and the function i try to call (with basic authentication attribute)</h2>
  
  <p>**</p>
  
  <pre><code>    [HttpGet]
      [ActionName("Get_UserID")]
      [IdentityBasicAuthentication]
      [Authorize]
      public HttpResponseMessage Get_UserID(string userName)
      {
          HttpResponseMessage res = new HttpResponseMessage(HttpStatusCode.Created);
          try
          {
              var user = Membership.GetUser(userName, false);
              if (user != null)
              {
                  res = Request.CreateResponse(HttpStatusCode.OK, (Guid)user.ProviderUserKey);
              }
              else
              {
                  res = Request.CreateResponse(HttpStatusCode.ExpectationFailed);
                  res.Content = new StringContent("Error");
                  res.ReasonPhrase = "UserName not find in the database";
              }
          }
          catch (Exception exc)
          {
              //Set the response message as an exception
              res = Request.CreateResponse(HttpStatusCode.InternalServerError);
              res.Content = new StringContent("Exception");
              res.ReasonPhrase = exc.Message;
          }
          return res;
      }
  </code></pre>
  
  <p>**</p>
  
  <h2>Client side - How i call the web api and build my httpClient</h2>
  
  <p>**</p>
  
  <pre><code>    public static async Task&lt;HttpResponseMessage&gt; RequestStart(string requestUrl, string webApiUrlBase = Globals.WebApi_Url, bool IsAuthenticateMemberRequest = false)
      {
          if (webApiUrlBase == null)
          {
              webApiUrlBase = Globals.WebApi_Url;
          }
          var response = new HttpResponseMessage(HttpStatusCode.Created);
  
          using (var client = new HttpClient())
          {
              if (IsAuthenticateMemberRequest)
              {
                  string strToEncode = ApplicationData.Current.LocalSettings.Values["userName"].ToString() + ":" + ApplicationData.Current.LocalSettings.Values["password"].ToString();
                  var authenticationBytes = Encoding.ASCII.GetBytes(strToEncode);
  
                  client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",
                  Convert.ToBase64String(authenticationBytes));
              }
              client.BaseAddress = new Uri(Globals.WebApi_Url);
              client.DefaultRequestHeaders.Accept.Clear();
              client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
              response = await client.GetAsync(requestUrl);
          }
  
          return response;
      }
  </code></pre>
  
  <p>**</p>
  
  <h2>IIS Configuration (appPool => NetworkServices - integrate)</h2>
  
  <p>**
  <a href="https://i.stack.imgur.com/gsnK3.png" rel="noreferrer"><img src="https://i.stack.imgur.com/gsnK3.png" alt="enter image description here"></a></p>
  
  <p>**</p>
  
  <h2>Fiddler Debug</h2>
  
  <p>**
  <a href="https://i.stack.imgur.com/rrv9k.png" rel="noreferrer"><img src="https://i.stack.imgur.com/rrv9k.png" alt="enter image description here"></a></p>