title: C# Get Access Token for Azure AD Identity
body: <p>I try to get an access token for an identity to get data from all users profiles. I'm using OpenID connect to authenticate the user, in which I succeeded. I'm also able to get an access token, but it is not valid.</p>
  
  <p>The code I'm using:
  To authenticate: </p>
  
  <pre><code>app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions()
          {
              ClientId = AppVar.ClientId,
              ClientSecret = AppVar.ClientSecret,
              Authority = AppVar.AzureADAuthority,
              RedirectUri = "https://localhost:44326/",
              ResponseType = "code id_token",    
              Notifications = new OpenIdConnectAuthenticationNotifications()
              {
                  AuthorizationCodeReceived = (context) =&gt; {
                      var code = context.Code;
                      ClientCredential credential = new ClientCredential(AppVar.ClientId, AppVar.ClientSecret);
                      string tenantID = context.AuthenticationTicket.Identity.FindFirst("http://schemas.microsoft.com/identity/claims/tenantid").Value;
                      string signedInUserID = context.AuthenticationTicket.Identity.FindFirst(ClaimTypes.NameIdentifier).Value;
                      ADALTokenCache cache = new ADALTokenCache(signedInUserID);
                      AuthenticationContext authContext = new AuthenticationContext(string.Format("https://login.windows.net/{0}", tenantID), cache);
                      AuthenticationResult result = authContext.AcquireTokenByAuthorizationCode(
                                 code, new Uri(HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Path)), credential, AppVar.AzureResource);
                      return Task.FromResult(0);
                  }
              }
          });
  </code></pre>
  
  <p>To acquire an access token for <a href="https://graph.microsoft.com" rel="nofollow noreferrer">https://graph.microsoft.com</a></p>
  
  <pre><code>public ActionResult Index()
      {
          string usrObjectId = ClaimsPrincipal.Current.FindFirst(AppVar.ClaimTypeObjectIdentifier).Value;
          AuthenticationContext authContext = new AuthenticationContext(AppVar.AzureADAuthority, new ADALTokenCache(usrObjectId));
          ClientCredential credential = new ClientCredential(AppVar.ClientId, AppVar.ClientSecret);
          AuthenticationResult res = authContext.AcquireToken(AppVar.AzureResource, credential);
  
          var client = new RestClient("https://graph.microsoft.com/v1.0/users/?$select=userPrincipalName,displayName,mobilePhone");
          var request = new RestRequest(Method.GET);
  
  
  
          request.AddHeader("Cache-Control", "no-cache");
          request.AddHeader("Authorization", "Bearer " + res.AccessToken);
          IRestResponse response = client.Execute(request);
          return View();
      }
  </code></pre>
  
  <p>But when I execute the request, I get:</p>
  
  <p><code>{
    "error": {
      "code": "InvalidAuthenticationToken",
      "message": "Access token validation failure.",
      "innerError": {
        "request-id": "1cc9e532-bd31-4ca5-8f1d-2d0796883c2e",
        "date": "2018-10-17T06:50:35"
      }
    }
  }</code></p>
  
  <p>What am I doing wrong?</p>