Using Azure managed Identities to access Azure SQL DB

二次信任 提交于 2021-01-07 04:57:06

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!