title: AuthenticationContext.AcquireTokenAsync
body: <p>I would like to be programmatically able to get a token from Azure. </p>
  
  <p>I call <code>GetAToken().Wait();</code> and it fails.</p>
  
  <p>and the method is:</p>
  
  <pre><code>public async Task&lt;string&gt; GetAToken()
  {
      // authentication parameters
      string clientID = "*********";
      string username = "&lt;azure login&gt;";
      string password = "&lt;azure login password&gt;";
      string directoryName = "&lt;AD Domain name&gt;";
  
      ClientCredential cc = new ClientCredential(clientID, password);
      var authenticationContext = new AuthenticationContext(
             "https://login.windows.net/" + directoryName);
  
      AuthenticationResult result = await authenticationContext.AcquireTokenAsync(
             "https://management.core.windows.net/", cc);
  
      if (result == null)
      {
          throw new InvalidOperationException("Failed to obtain the JWT token");
      }
  
      string token = result.AccessToken;
  
      return token;
  }
  </code></pre>