Search code examples
entity-framework-coreazure-functionsazure-sql-databaseazure-managed-identity

Calling a stored procedure from EF Core 8 with Managed Identities


I have developed an Azure Function app that connects to a Azure SQL Server through EF Core 8. I use a system assigned identity.

When connecting to tables etc everything goes as expected. But when I call a stored procedure it fails with following:

Login failed for user token-identified principal

When I debug locally with my own user everything works.

I have granted access with following SQL:

GRANT EXECUTE ON dbo.XX_InitRun TO [xx-int-xx-dev];

This is how I call the stored procedure:

public void XX_InitRun()
{            
   this.Database.ExecuteSqlRaw("[dbo].[XX_InitRun]");
}

Any suggestions how to troubleshoot or solve this?


Solution

  • Follow the instructions here. If you can't connect, you probably don't have the contained database user created for the managed identity. eg:

    CREATE USER [<Managed Identity Name>] FROM EXTERNAL PROVIDER

    Tutorial: Use a Windows VM system-assigned managed identity to access Azure SQL