I’m trying to configure an Azure Function to access an Azure SQL Database using Managed Identity. I’ve followed the official steps to enable a system-assigned Managed Identity for my Function App and granted the identity db_datareader
permissions on the database.
Here’s the setup:
Azure Function is deployed in a consumption plan.
Managed Identity is enabled at the Function App level.
SQL Server firewall rules allow access for Azure services.
var credential = new DefaultAzureCredential();
var sqlConnection = new SqlConnection(new SqlConnectionStringBuilder
{
DataSource = "myserver.database.windows.net",
InitialCatalog = "mydatabase",
Authentication = SqlAuthenticationMethod.ActiveDirectoryManagedIdentity
}.ToString());
sqlConnection.AccessToken = credential.GetToken(
new TokenRequestContext(new[] { "https://database.windows.net/.default" })
).Token;
await sqlConnection.OpenAsync();
Azure Function fails with this error:
Error: 404 Resource Not Found: Could not retrieve token from Managed Identity. Ensure the resource is properly assigned.
What I’ve checked:
Managed Identity is listed under the Function App’s Identity settings.
SQL Server logs do not show any attempt to connect.
Verified the Function App has sufficient permissions on the SQL database.
Akash Panchani is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.