Azure-Samples / Azure-Time-Series-Insights

Sample modules written in different languages that can be used as templates for applications needing to query Time Series Insights
MIT License
21 stars 31 forks source link

Managed identity auth with DefaultAzureCredentials() and token refresh? #33

Open MichalLechowski opened 3 years ago

MichalLechowski commented 3 years ago

I'm getting a bit lost in what's the proper way to auth to TSI. There are examples for many libraries and MSAL/ADAL approaches. My scenario is pretty simple, I have AppService in Azure which is REST WebAPI (.NET Core 3.1 that will be upgraded to .NET 5) that collects the data from TSI. The code for getting token using Azure.Identity is:

image

What I am not sure of is how should I refresh the token correctly. Is the manual call to GetToken when the current one expires is the proper way when using Managed Identity to access TSI? Or is there any fancier way, maybe I shouldn't use Azure.Identity but something else? Thanks for any suggestions.

My solution based on example AutoRest code for Gen2: Default code: image

Renew token: image

I've added the TokenExpiresOn property to TimeSeriesInsightsClient class and also update the value when new token is assigned.

I have no idea if that's the best approach but it works.