title: Azure AD API request 401 Unauthorized
body: <p>I have a standard Web API running on an Azure website with Azure AD authentication enabled, when browsing to the API in a browser I am able to login via the browser and gain access to the API. </p>
  
  <p>The WPF desktop application however is receiving an <strong>Unauthorized</strong> response when submitting the request:</p>
  
  <pre><code>var authContext = new AuthenticationContext(authority, new FileCache());
  var accessToken = await authContext.AcquireTokenAsync(apiResourceid, clientId, redirectUri,
                      new PlatformParameters(PromptBehavior.Auto));
  // accessToken is valid
  
  var apiUrl = "https://example.azurewebsites.net/api/list";
  var request = new HttpRequestMessage(HttpMethod.Get, apiUrl);
  httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken.AccessToken);
  var response = await httpClient.SendAsync(request);
  </code></pre>
  
  <p>The authentication is successfull and I can see the User info when debugging. </p>
  
  <p>I do not have access to the Azure account but am confident the Service AD application is configured correctly to allow access to the Client AD application as when testing on an alternate account (not configured correctly) the <code>AuthenticationContext.AcquireTokenAsync</code> method was failing.</p>
  
  <p><del>I did notice that the <code>AuthenticationResult.ExpiresOn</code> is always in the past but see no way of extending it, should this be a future date?</del> - (Time is of course UTC)</p>
  
  <p>Request:</p>
  
  <pre><code>GET https://example.azure
  websites.net/api/categorisation HTTP/1.1
  Authorization: Bearer eyJ0eXAiO...
  Host: example.azurewebsites.net
  </code></pre>
  
  <p>Response:</p>
  
  <pre><code>HTTP/1.1 401 Unauthorized
  Content-Length: 58
  Content-Type: text/html
  Server: Microsoft-IIS/8.0
  WWW-Authenticate: Bearer realm="example.azurewebsites.net"
  X-Powered-By: ASP.NET
  Set-Cookie: ARRAffinity=e35f2977dba55e6708887e762940f75c2a0fcb0a9df4e1cbe0d3f10a614c59b8;Path=/;Domain=example.azurewebsites.net
  Date: Fri, 08 Jul 2016 07:51:13 GMT
  
  You do not have permission to view this directory or page.
  </code></pre>
  
  <p>Update:</p>
  
  <p>I have recreated the environment in an Azure account I have access to and still receive an Unauthorised response (works fine in a browser).</p>