我正在运行一个关于如何设置对 Azure SQL 的 MSI 访问的 Microsoft 文档教程。本文: https ://docs.microsoft.com/en-gb/azure/app-service/app-service-web-tutorial-connect-msi
我成功地从我的 Azure Web 配置管理器中获取了连接字符串
public MyDatabaseContext(SqlConnection conn) : base(conn, true)
{
conn.ConnectionString = WebConfigurationManager.ConnectionStrings["dbConnectionName"].ConnectionString;
// DataSource != LocalDB means app is running in Azure with the SQLDB connection string you configured
if (conn.DataSource != "(localdb)\\MSSQLLocalDB")
conn.AccessToken = (new AzureServiceTokenProvider()).GetAccessTokenAsync("https://database.windows.net/").Result;
Database.SetInitializer<MyDatabaseContext>(null);
}
我在我的控制器中使用
private MyDatabaseContext db = new MyDatabaseContext(new SqlConnection());
当我最终拨打电话时,例如:
var sample = (from c in _context.Customer where c.Abbreviation == abbrev.Trim() select c).FirstOrDefault();
我收到一个错误 System.Data.Entity.Core.EntityException:“底层提供程序在打开时失败。” “SqlException:用户 'NT AUTHORITY\ANONYMOUS LOGON' 登录失败。”
即使我这样做也会发生这种情况(https://docs.microsoft.com/en-gb/azure/app-service/app-service-managed-service-identity#obtaining-tokens-for-azure-resources)
using Microsoft.Azure.Services.AppAuthentication;
using Microsoft.Azure.KeyVault;
// ...
var azureServiceTokenProvider = new AzureServiceTokenProvider();
string accessToken = await
azureServiceTokenProvider.GetAccessTokenAsync("https://vault.azure.net");
// OR
var kv = new KeyVaultClient(new KeyVaultClient.AuthenticationCallback(azureServiceTokenProvider.KeyVaultTokenCallback));
并在我的 SqlConnection 中填充适当的访问令牌。
任何帮助,将不胜感激