title: how to get AccessToken to access web api secured with Azure AD
body: <p>I have a web api application (<a href="http://localhost:54540" rel="nofollow noreferrer">http://localhost:54540</a>) secured with Azure AD like below,</p>
  
  <p><a href="https://i.stack.imgur.com/Yk0uK.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Yk0uK.png" alt="enter image description here"></a></p>
  
  <p>Now I am trying to access web api from a regular MVC application using <code>OpenIdConnect</code>, here is the <code>startup</code> class for MVC,</p>
  
  <pre><code>public partial class Startup
  {
      private static string clientId = ConfigurationManager.AppSettings["ida:ClientId"];
      private static string aadInstance = ConfigurationManager.AppSettings["ida:AADInstance"];
      private static string tenantId = ConfigurationManager.AppSettings["ida:TenantId"];
      private static string postLogoutRedirectUri = ConfigurationManager.AppSettings["ida:PostLogoutRedirectUri"];
      private static string authority = aadInstance + tenantId;
  
      public void ConfigureAuth(IAppBuilder app)
      {
          app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
  
          app.UseCookieAuthentication(new CookieAuthenticationOptions());
  
          app.UseOpenIdConnectAuthentication(
              new OpenIdConnectAuthenticationOptions
              {
                  ClientId = clientId,
                  Authority = authority,
                  PostLogoutRedirectUri = postLogoutRedirectUri
              });
      }
  }
  </code></pre>
  
  <p>Now, I am trying to access web api into one of MVC controller like below,</p>
  
  <pre><code>[Authorize]
  public class HomeController : Controller
  {
      public string AccessToken
      {
          get
          {
              //var claim = Context.User?.FindFirst("access_token");
              //if (claim == null)
              //{
              //    throw new InvalidOperationException();
              //}
  
              //return claim.Value;
          }
      }
  
      public async Task&lt;ActionResult&gt; Index(CancellationToken cancellationToken)
      {
  
          using (var client = new HttpClient())
          {
              var request = new HttpRequestMessage(HttpMethod.Get, "http://localhost:54540/api/message");
              request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", AccessToken);
  
              var response = await client.SendAsync(request, cancellationToken);
              response.EnsureSuccessStatusCode();
  
  
              return View();
          }
  
  
  
      }
  } 
  </code></pre>
  
  <p>Question - what code is required to get <code>AccessToken</code>? Thanks!</p>