title: Authorization has been denied for this request in AAD
body: <p>I am new to Azure Active Directory development. We have few services which need to be accessed by web application and mobile devices(cordova based application). </p>
  
  <ul>
  <li>For Web application we are using <a href="https://github.com/AzureAD/azure-activedirectory-library-for-js" rel="nofollow">ADAL JS</a>.</li>
  <li>For mobile application we are using <a href="https://azure.microsoft.com/en-in/documentation/articles/active-directory-devquickstarts-cordova/" rel="nofollow">Azure AD with an Apache Cordova
  app</a>.</li>
  </ul>
  
  <h2>Issue</h2>
  
  <p>The token generating with ADAL JS is authorizing over the web services and working as expected. But the token generating with Azure AD is throwing respective error </p>
  
  <blockquote>
    <p>Authorization has been denied for this request</p>
  </blockquote>
  
  <h2>Workaround</h2>
  
  <p>We tried to get access token using user credentials in C#. We are successful in achieving it. Using that access token we tried to hit our services in Fiddler. Even then the same error was thrown.</p>
  
  <blockquote>
    <p>Authorization has been denied for this request</p>
  </blockquote>
  
  <h2>Code Samples</h2>
  
  <p>Cordova Code</p>
  
  <pre><code>    var authority = 'https://login.windows.net/**tenantName**';
  var resourceUrl = 'https://graph.windows.net/';
  var clientID = '***CLIENT ID IN NATIVE APP***';
  var redirectUrl = 'http://localhost:49906/';
  var tenantName = '***TENANT NAME***';
  var endpointUrl = resourceUrl + tenantName;
  
  createContext: function () {
              AuthenticationContext.createAsync(authority)
              .then(function (context) {
                  mapp.authContext = context;
                  mapp.log("Created authentication context for authority URL: " + context.authority);
                  mapp.acquireToken();
              }, mapp.error);
          },
          acquireToken: function () {
              if (mapp.authContext == null) {
                  mapp.error('Authentication context isn\'t created yet. Create context first');
                  return;
              }
              mapp.authContext.acquireTokenSilentAsync(resourceUrl, clientID).then(function (success) {
                  console.log("INSIDESILENT");
                  mapp.error("Failed to acquire token: " + success);
                  console.log("DATA:::: "+success);
              }, function () {
                  mapp.authContext.acquireTokenAsync(resourceUrl, clientID, redirectUrl)
                  .then(function (authResult) {
                      mapp.log('Acquired token successfully: ' + pre(authResult));
                      console.log("DATA:::"+authResult.accessToken);
                      localStorage.setItem("SSOFlag", "true");
                      angular.bootstrap(document, ['keurapp']);
                      userDetail = JSON.parse(localStorage.getItem("userDetails"));
                  }, function (err) {
                      mapp.error("Failed to acquire token: " + pre(err));
                  });
              });
          }
  </code></pre>
  
  <p>C# Code</p>
  
  <pre><code>public static string GetAccessToken()
          {
              AuthenticationContext authenticationContext = new AuthenticationContext("https://login.microsoftonline.com/**tenantName**", true);
              UserCredential clientCred = new UserCredential("***USERID***", "***PASSWORD***");
              AuthenticationResult authenticationResult = authenticationContext.AcquireToken(resource: "***APP ID URI OF WEB APPLICATION***", clientId: "***CLIENT ID NATIVE APP***", userCredential: clientCred);
              token = authenticationResult.AccessToken;
              return token;
          }
  </code></pre>
  
  <p>Services Samples:</p>
  
  <pre><code>//[EnableCors(origins: "*", headers: "*", methods: "*")]
  //[Authorize]
  public class RepositoryController : ApiController
  {
      //With Few methods
  }
  
  public void Configuration(IAppBuilder app)
  {
      ConfigureOAuth(app);
      HttpConfiguration config = new HttpConfiguration();
      WebApiConfig.Register(config);
      app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
      app.UseWebApi(config);
  }
  </code></pre>
  
  <p>Looking out for help</p>