title: Microsoft.Data.SqlClient.SqlException (0x80131904): Login failed for user '<token-identified principal>'. Token is expired
body: <p>I am using Entity Framework to connect to an Azure SQL database and I'm connecting using an access token (obtained via the Managed Identities).</p>
  <p>I am not using user name and password in the SQL connection string and using managed identity and keeping SQL connection string in Azure keyvault.</p>
  <p>I'm connecting to the database with a token like shown below, but after an hour it expires, so I'm getting &quot;SQL login failed - &quot; errors.</p>
  <p>I have a long running process once connected the the database, and it collects some config data from some tables, and after more than 45 minutes, I need to update data in the database. When trying to save changes using <code>dbcontext</code>, the code is throwing this timeout issue.</p>
  <p>How to get a new refreshed token and set to connection?</p>
  <pre><code>public SqlConnection GetDBConnection()
  {
      SqlConnection conn = new SqlConnection();
      conn.ConnectionString = Environment.GetEnvironmentVariable(&quot;SqlConnectionString&quot;);
  
      var credential = new DefaultAzureCredential();
      var accessToken = credential.GetToken(new TokenRequestContext(new[] { &quot;https://database.windows.net/.default&quot; }));
  
      conn.AccessToken = accessToken.Token;
  
      return conn;
  }
  </code></pre>