title: ADAL native application token expires after 12 hours
body: <p>I've created the .net console application which is the client of my web api. Both apps are registered in azure. I want my console app to run without user interaction. Console app checks the message queue and if the message arrives, it does some calculation and sends back the data to my web api. I use adal to authenticate my connection. I authenticate by secret key. Since my client uses generated code by AutoRest I added DelegatingHandler to catch every request and add the authorization header before i send it:</p>
  
  <pre><code>public class ClientHandler : DelegatingHandler
  {
      protected async override Task&lt;HttpResponseMessage&gt; SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
      {
          AuthenticationContext authContext = Constants.authContext;
          ClientCredential clientCredential = Constants.clientCredential;
          string apiId = Constants.apiId;
          string tokenType = Constants.tokenType;
  
          // ADAL includes token in memory cache, so this call will only send a message to the server if the cached token is expired.
          var result = await authContext.AcquireTokenAsync(apiId, clientCredential);
          request.Headers.Authorization = new AuthenticationHeaderValue(tokenType, result.AccessToken);
  
          return await base.SendAsync(request, cancellationToken);
      }
  }
  </code></pre>
  
  <p>As you can see I am using already defined authorization context. Thanks to code above I can get the token without user interaction. And this work just fine! <strong>However</strong> after <strong>12 hours</strong> the application starts returning <code>Unauthorized</code> error. The question is how do I prevent it? I thought that <code>AcquireToken</code> method takes care of the token expiration. Am I missing something?</p>
  
  <p><strong>EDIT:</strong>
  Constant class:</p>
  
  <pre><code>public static class Constants
  {
      public static string aadInstance = ConfigurationManager.AppSettings["aadInstance"];
      public static string tenant = ConfigurationManager.AppSettings["aadTenantName"];
      // this application id
      public static string clientId = ConfigurationManager.AppSettings["clientApi:ClientId"];
      // the key which it can be authenticated
      public static string appKey = ConfigurationManager.AppSettings["clientApi:AppKey"];
      // the id of the api
      public static string apiId = ConfigurationManager.AppSettings["apiId"];
      public static string authority = String.Format(CultureInfo.InvariantCulture, aadInstance, tenant);
      public static string tokenType = ConfigurationManager.AppSettings["TokenType"];
      public static AuthenticationContext authContext = null;
      public static ClientCredential clientCredential = null;
  
      public static async Task&lt;TokenCredentials&gt; Authenticate()
      {
          authContext = new AuthenticationContext(authority);
          clientCredential = new ClientCredential(clientId, appKey);
          var result = await authContext.AcquireTokenAsync(apiId, clientCredential);
          return new TokenCredentials(result.AccessToken, tokenType);
      }
  }
  </code></pre>