title: Azure Service principal authentication for API App
body: <p>I've build a simple API App with a REST Service. Now I've enabled principal authentication in Azure App Service. I'm able to get my bearer token by C# code:</p>
  
  <pre><code>public static AuthenticationResult GetS2SAccessTokenForProdMSA()
  {
          return GetS2SAccessToken(authority, resource, clientId, clientSecret);
  }
  
  static AuthenticationResult GetS2SAccessToken(string authority, string resource, string clientId, string clientSecret)
  {
          var clientCredential = new ClientCredential(clientId, clientSecret);
          AuthenticationContext context = new AuthenticationContext(authority, false);
          AuthenticationResult authenticationResult = context.AcquireTokenAsync(resource, clientCredential).Result;
          return authenticationResult;
  }
  </code></pre>
  
  <p>If I want to get my bearer token by JavaScript, e.g.:</p>
  
  <pre><code>$.ajax({
      url: 'https://login.microsoftonline.com/1640e15a-2d4c-4903-8b89-a00c52ac3c17/oauth2/token',
      type: 'POST',
      crossOrigin: true,
      data: 'resource=https://foobar' +
                  '&amp;client_id=68b9a002-d6f9-4732-9c9c-893b2c60ba42' +
                  '&amp;client_secret=&lt;secret&gt;' +
                  '&amp;grant_type=client_credentials',
      contentType: 'application/x-www-form-urlencoded',
      success: function (returndata) {
          alert(formData);
      },
      error: function (errordata) {
          alert(errordata.statusText);
      }
  });
  </code></pre>
  
  <p>I get an error in Chrome saying:</p>
  
  <blockquote>
    <p>No 'Access-Control-Allow-Origin' header is present on the requested
    resource</p>
  </blockquote>
  
  <p>But in Fiddler I can see the successful answer with my bearer token (same error in Firefox, but not in IE 11).</p>
  
  <p>Why is it not possible to request the token by an AJAX request?</p>
  
  <p>Am I missing something?</p>
  
  <p>Cheers</p>