question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[FEATURE REQ]Azure.Identity should handle token caching and refreshing when calling .GetTokenAsync just like AppAuthentication does

See original GitHub issue

I was reading this documentation and getting ready to migrate from AppAuthentication to Azure.Identity.

While the new library surely works amazingly well with the new Azure client SDKs, I soon found out that the method to directly obtain access token, TokenCredential.GetTokenAsync, has the following warning:

// This method is called automatically by Azure SDK client libraries. You may call
// this method directly, but you must also handle token caching and token refreshing.

There are plenty of use cases where a managed identity access token needs to be explicitly retrieved. For example, using Entity Framework Core with either Azure SQL or Azure PostgreSQL with managed identity requires that the actual tokens to be passed to the EF provider. Same with authenticating against a custom Azure AD enterprise application via standard HttpClient.

These scenarios are unlikely to get native integration with Azure.Identity anytime soon.

The older library’s AzureServiceTokenProvider.GetAccessTokenAsync has built-in token caching and refreshing which works fairly well. It would be great if Azure.Identity can implement the same. Only then it will become a viable full replacement for AppAuthentication.

In the meantime, I believe this difference should be noted in the migration documentation under the “Access token retrieval” section.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:9
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
felinepccommented, Oct 3, 2021

Dear awesome Microsoft engineers, by any chance we can give this a higher priority? I just did a search for “cache” on this repo and saw a long list of requests asking more or less the same thing: cache_request

Identity is a huge issue and we do NOT want to stick to the old library. Azure.Identity is not going to create libraries anytime soon for things like Azure SQL/Azure PostgreSQL/Azure App Service Microsoft Authentication, so we need to access this access token for the time being.

The old Azure.Services.AppAuthentication package works very reliably for this and has all the beautiful Microsoft-written caching logic built in. But as we all know since the old package is EOL, we’d really love to move to Azure.Identity, as Microsoft strongly suggested themselves. But we really do need this key feature implemented.

2reactions
felinepccommented, Apr 8, 2021

@leidegre Sorry I started with EF Core and never used EF6, but it does appear to have IDbConnectionInterceptor? See this and this.

Yeah I know a simple in-memory cache and refresh logic wouldn’t take more than a few lines of code, but I just felt that the new package should have this built-in because the old one had it, and I generally trust MS’s internal implementation on this sort of things more than my own.

Read more comments on GitHub >

github_iconTop Results From Across the Web

DefaultAzureCredential.GetTokenAsync Method
Acquired tokens are cached by the credential instance. Token lifetime and refreshing is handled automatically. Where possible, reuse credential instances to ...
Read more >
how to cache and refresh managed identity token
The caching is implemented within the other SDKs (such as Azure storage, KeyVault etc) when they call getToken.
Read more >
InteractiveBrowserCredential.GetTokenAsync Method
Acquired tokens are cached by the credential instance. Token lifetime and refreshing is handled automatically. Where possible, reuse credential instances to ...
Read more >
How do you handle authentication and token refresh with ...
I found a reference here explaining that these session token caches have a scoped lifetime and should not be used when TokenAcquisition is ......
Read more >
Failed to refresh access token in VisualStudio
Exception Message: Tried to get token using Azure CLI. Access token could not be acquired. 'az' is not recognized as an internal or...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found