title: Using OnAuthorizationCodeReceived to retrieve Azure GraphAPI AccessToken
body: <p>I am currently using code in my pipeline to cache the bearer token for the Graph API using Azure AD. This code was ported from a working ASP.NET 4 application, but it feels like the new OpenIdConnectOptions in Core should make this easier. Is there a more direct call that I can use in the OnAuthorizationCodeReceived event that will use the AuthenticationContext to cache the token once the code is received?  Here is my current code:</p>
  
  <pre><code>var azureSettings = app.ApplicationServices.GetService&lt;IOptions&lt;AzureSettings&gt;&gt;().Value;
  app.UseOpenIdConnectAuthentication(new OpenIdConnectOptions
  {
      ClientId = azureSettings.ClientId,
      ClientSecret = azureSettings.AppKey,
      Authority = string.Format(azureSettings.AadInstance, azureSettings.TenantId),
      Resource = azureSettings.GraphResourceUri,
      ResponseType = OpenIdConnectResponseType.CodeIdToken,
      TokenValidationParameters = new TokenValidationParameters
      {
          RoleClaimType = "roles"
      },
      Events = new OpenIdConnectEvents()
      {
          OnAuthorizationCodeReceived = (context) =&gt;
          {
              string resourceUri = azureSettings.GraphResourceUri;
              var authContext = new AuthenticationContext(context.Options.Authority);
              var credential = new ClientCredential(context.TokenEndpointRequest.ClientId, context.TokenEndpointRequest.ClientSecret);
              var result = authContext.AcquireTokenByAuthorizationCodeAsync(context.TokenEndpointRequest.Code, new Uri(context.TokenEndpointRequest.RedirectUri), credential, resourceUri);
  
              context.HandleCodeRedemption(result.AccessToken, result.IdToken);
          }
      }
  });
  </code></pre>
  
  <p>The above code works just fine, but it feels like I am duplicating a lot of code just to submit what is mostly contained inside of the AuthorizationCodeReceivedContext already.</p>
  
  <p>Is there an easier way that I am simply overlooking?</p>