问题
Is there a way to use Azure managed identities with Linux VMs to access Azure SQL DB? All I could find is this document https://docs.microsoft.com/en-us/azure/active-directory/managed-identities-azure-resources/tutorial-windows-vm-access-sql which specifically speaks to Windows VMs. Is there a documented step-by-step approach for a Linux machine?
回答1:
SQL access using Managed Identity from Linux webapp is supported. The Use a Windows VM system-assigned managed identity to access Azure SQL tutorial is pretty much applicable to Linux, just dismiss the code sample and use something like this:
var azureServiceTokenProvider = new AzureServiceTokenProvider();
var accessToken = await azureServiceTokenProvider.GetAccessTokenAsync("https://database.windows.net/");
using
var sqlConnection = new SqlConnection(configuration.GetConnectionString("Default")) {
AccessToken = accessToken
};
using
var sqlCommand = new SqlCommand("SELECT @@VERSION", sqlConnection);
await sqlConnection.OpenAsync();
var version = (string) await sqlCommand.ExecuteScalarAsync();
Full code available here, just replace the connection string with yours.
回答2:
It seems the managed identity does not support for Linux VMs to access Azure SQL DB.
There is a similar issue here. And there is a workaround which uses the cross-platform .NET core libraries, you could refer to it.
来源:https://stackoverflow.com/questions/62003073/using-azure-managed-identities-to-access-azure-sql-db