.NetCore API 无法使用 MSI 从 Azure CLI 获取令牌

.NetCore API fails to get token from Azure CLI using MSI

我有几个 API,我正在改变我访问 Azure SQL 数据库的方式,从简单的凭证到利用我们的 Azure Active 的托管服务标识模型目录。

我对我的旧 .Net API 执行此操作没有问题,但我无法让我的新 .NetCore API 执行相同的操作。我读过 .NetCore 尚不支持的可能性,但我不清楚这是 accurate/true 还是不支持。

我运行这个代码:

SqlConnection conn = new SqlConnection(_ConnectionString);
conn.AccessToken = (new AzureServiceTokenProvider()).GetAccessTokenAsync("https://database.windows.net/").Result;

并得到这个错误:

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. 

我不希望它通过 MSI 工作,因为我 运行 在本地安装它,但它应该通过方法 2

工作

*更新 找到这篇相关文章:https://github.com/Azure/azure-sdk-for-net/issues/3933

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.

简而言之,它在 .Net Core 2.1 中不支持

This scenario is currently supported by .NET Framework 4.6 and above, but not by .NET Core 2.1. .NET Core 2.2 does support the scenario, but is not yet included in the default images in App Service.

更多的细节,你可以参考这篇article