title: How to get Dynamics 365 Token?
body: <p>I trying to get Dynamics 365 token but the method for obtain the token is asynchronous and when I try to return the token I obtain a 500 Internal Server Error. Is there another way to get Dynamics 365 token? Or how can I show the token on return of the controller?</p>
  <p>My get token method:</p>
  <pre><code> public static async Task&lt;string&gt; AccessTokenGenerator()
      {
          string clientId = &quot;myClientId&quot;;
          string clientSecret = &quot;myClientSecret&quot;;
          string authority = &quot;https://login.microsoftonline.com/9dfdf489-e992-46ea-9fc7-317dfc206add&quot;;
          string resourceUrl = &quot;https://devtreinamento.crm2.dynamics.com&quot;; // Org URL  
  
          ClientCredential credentials = new ClientCredential(clientId, clientSecret);
          var authContext = new Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext(authority);
          var result = await authContext.AcquireTokenAsync(resourceUrl, credentials);
          return result.AccessToken;
      }
  </code></pre>
  <p>My show token method:</p>
  <pre><code>[Route (&quot;&quot;)]
      [HttpGet]
      public  ActionResult showToken()
      {
          var token = getToken.AccessTokenGenerator();
  
          return Ok(token);
      }
  </code></pre>