Q:

Using Azure managed Identities to access Azure SQL DB

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?

A:

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.

