title: Azure ExpiredAuthenticationToken when fetching the Azure data Factory information by C# console application via Visual Studio
body: <p>I have written a console application which could fetch all information about Azure datafactory for a given azure subscription and resource group. I am using Microsoft provided .NET API.
  The code runs for a while as it monitors all the data factories and pipelines and put it into azure SQL database.
  I am getting following exception when code runs for more than a hour: 
  <a href="https://i.stack.imgur.com/tcwft.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/tcwft.png" alt="token expire exception"></a></p>
  
  <p>I am using following code to generate the to</p>
  
  <pre><code> public static async Task&lt;string&gt; GetAuthorizationHeader ()
          {
          AuthenticationContext context = new AuthenticationContext(ConfigurationManager.AppSettings["ActiveDirectoryEndpoint"] +
              ConfigurationManager.AppSettings["ActiveDirectoryTenantId"]);
          ClientCredential credential = new ClientCredential(
              ConfigurationManager.AppSettings["ApplicationId"],
              ConfigurationManager.AppSettings["Password"]);
          AuthenticationResult result = await context.AcquireTokenAsync(
              resource: ConfigurationManager.AppSettings["WindowsManagementUri"],
              clientCredential: credential);
  
          if ( result != null )
              return result.AccessToken;
  
          throw new InvalidOperationException("Failed to acquire token");
          }
  
  public static TokenCloudCredentials GetTokenCloudCredentials ()
          {
          return new TokenCloudCredentials(ConfigurationManager.AppSettings["SubscriptionId"],
              GetAuthorizationHeader().Result);
          }
  </code></pre>
  
  <p>This code is available at microsoft website <a href="https://docs.microsoft.com/en-us/azure/data-factory/v1/data-factory-copy-activity-tutorial-using-dotnet-api" rel="nofollow noreferrer">here</a>.</p>
  
  <p>Is there any way to increase the expiration time of token?
  or
  How do I refresh the authentication token?</p>
  
  <p>Thanks,
  Jai</p>