I have a couple APIs and I'm in the process of changing the way I access my Azure SQL Databases from a simple credential to a Managed Service Identity model leveraging our Azure Active Directory.
I've had no problem doing this for my older .Net API but I cannot get my newer .NetCore API to do the same thing. I've read the possibility that .NetCore does not support this yet but it's not clear to me if this is accurate/true or not.
I run this code:
SqlConnection conn = new SqlConnection(_ConnectionString);
conn.AccessToken = (new AzureServiceTokenProvider()).GetAccessTokenAsync("https://database.windows.net/").Result;
and get this error:
AzureServiceTokenProviderException: Parameters: Connectionstring: [No connection string specified], Resource: https://database.windows.net/, Authority: . Exception Message: Tried the following 3 methods to get an access token, but none of them worked.
Parameters: Connectionstring: [No connection string specified], Resource: https://database.windows.net/, Authority: . Exception Message: Tried to get token using Managed Service Identity. Unable to connect to the Managed Service Identity (MSI) endpoint. Please check that you are running on an Azure resource that has MSI setup.
Parameters: Connectionstring: [No connection string specified], Resource: https://database.windows.net/, Authority: . Exception Message: Tried to get token using Visual Studio. Access token could not be acquired. Visual Studio Token provider file not found at "C:\Users\simonb\AppData\Local\.IdentityService\AzureServiceAuth\tokenprovider.json"
Parameters: Connectionstring: [No connection string specified], Resource: https://database.windows.net/, Authority: . Exception Message: Tried to get token using Azure CLI. Access token could not be acquired.
I don't expect this to work through MSI because I'm running it locally but it should work through method 2
*update Found this related article: https://github.com/Azure/azure-sdk-for-net/issues/3933