ASP.NET Core App -> ASP.NET Core API -> Azure SQL Pass-through identification using MSAL

Copper Contributor

I have an ASP.NET Core 3.1 Razor Pages Web App calling an ASP.NET Core 3.1 MVC API, which in turn accesses an Azure SQL database on the user's behalf.


I have configured the application with MSAL (Microsoft.Identity.Web and Microsoft.Identity.Web.UI) as per various documents, and registered both the App and the API in AD. 


All works perfectly in the development environment - when I start both applications I get prompted to sign in with Microsoft ID, and the App receives an access token giving it access to the API. The API then uses this...

var token = await new AzureServiceTokenProvider().GetAccessTokenAsync("", _tenantId) obtain an access token for SQL. When I run it locally this token identifies me as the user and all works as expected.


However, when I deploy the API to an Azure App Service and then try the same thing, the access token it receives to access SQL contains an application identity, not a user identity. That's no good because I need to be able to use row-level security and auditing at the database level.


This has been driving me slightly crazy - can anyone point me at any document or tutorial that uses this exact tech stack (not the older AAD version) to achieve that (and which works in an Azure App Service)? Since it all works perfectly in the dev environment I'm starting to suspect this is either a bug or an out-of-date feature in App Services. 

0 Replies