A cloud-based identity and access management service for securing user authentication and resource access
The most common cause for an API that works locally but fails with 500 Internal Server Error after deployment to Azure App Service, when using Microsoft Entra ID access tokens, is a mismatch between how tokens are validated in Azure and how the app is configured (audience, issuer, or missing headers/middleware). Another frequent cause is misconfiguration when running behind a gateway/proxy.
Use the following checks and fixes:
- Check that the API receives a valid Bearer token in Azure
- Ensure the client calls the Azure-hosted API with an
Authorization: Bearer <token>header. - If the request has no token or an invalid token, the API can return 401 or 500 depending on error handling.
- Ensure the client calls the Azure-hosted API with an
- Verify audience (
aud) matches what the API expects- Decode the JWT at https://jwt.ms.
- Check the
audclaim. - Ensure it matches the API’s configured audience (for example,
api://<application-client-id>or the App ID URI configured for the API). - If the token is issued for a different resource, token validation fails and can surface as 401/500.
- Align API token validation with Entra configuration
- In the API’s
TokenValidationParameters, ensureValidAudience/ValidAudiencesandValidIssuer/ValidIssuersmatch the values from the Entra app registration:-
audmust match the API’s App ID URI or client ID. -
issmust match the tenant’s issuer (for example,https://login.microsoftonline.com/<tenant-id>/v2.0).
-
- If the API is behind a gateway or uses multiple URLs, add all valid audiences as shown in the gateway troubleshooting guidance.
- In the API’s
- If the API is behind a gateway or proxy, configure forwarded headers
- When deployed behind a gateway (Front Door, Application Gateway, API Management, etc.), missing forwarded headers can cause redirect or token validation issues.
- Ensure the middleware order includes forwarded headers before authentication:
app.UseForwardedHeaders(); app.UseAuthentication(); app.UseAuthorization(); - Verify the gateway forwards
X-Forwarded-ProtoandX-Forwarded-Host.
- Troubleshoot 401/500 by enabling detailed logging
- Use
JwtBearerEventsor Serilog-based logging to capture token validation errors and see why the API is failing in Azure. - Enable debug logging for
Microsoft.IdentityandMicrosoft.IdentityModelto see detailed token validation messages.
- Use
- Check for generic 401/500 causes
- A 401/500 can also occur if:
- The token is expired.
- The token is missing required claims.
- The token’s issuer or audience does not match the API configuration.
- Use the logging guidance to confirm which of these applies.
- A 401/500 can also occur if:
- If using a gateway, validate the audience and headers again
- For APIs behind gateways, explicitly check:
- The
audclaim matches one of the configured valid audiences (e.g.,api://your-client-id, backend URL, or gateway URL as configured). - Forwarded headers are correctly handled and
UseForwardedHeadersis configured before authentication.
- The
- For APIs behind gateways, explicitly check:
Applying these checks will typically reveal whether the issue is an audience/issuer mismatch, missing/misrouted headers, or a misconfigured authentication pipeline in Azure App Service.
References:
- 401 Unauthorized errors in ASP.NET Core Web API with Microsoft Entra ID
- Add Microsoft Entra ID authentication to a .NET Aspire app
- Use Serilog to troubleshoot Microsoft Entra protected Web API authentication or authorization errors
- Deploy protected APIs behind gateways
- Deploy web apps behind proxies and gateways
- App Authentication client library for .NET - version 1.6.0
- Tutorial: Connect an App Service app to SQL Database on behalf of the signed-in user