An Azure service that provides an event-driven serverless compute platform.
Hello Richard,
Thank you for reaching out on the Microsoft Q&A platform!
You are entirely correct that you shouldn't have to write custom JWT validation middleware from scratch. The recommended approach to secure an HTTP-triggered Azure Function using Microsoft Entra ID (formerly Azure AD) is to use the platform's built-in App Service Authentication and Authorization, commonly referred to as "Easy Auth."
By enabling this feature, Azure intercepts the incoming HTTP requests, validates the Bearer token in the Authorization header, and populates the user claims before the request ever reaches your function's code.
Here is what the official Microsoft documentation says about this feature:
"App Service provides built-in authentication and authorization capabilities (sometimes referred to as "Easy Auth"), so you can sign in users and access data by writing minimal or no code in your web app, RESTful API, and mobile back end, and also Azure Functions. This article describes how App Service helps simplify authentication and authorization for your app." (Reference: Authentication and authorization in Azure App Service and Azure Functions)
To implement this, you will follow these high-level steps:
- Create an App Registration for your API: In Microsoft Entra ID, register your Function App. Expose an API scope (e.g.,
api://<client-id>/access_as_user). - Configure App Service Authentication: Navigate to your Function App in the Azure Portal, select Authentication, and add Microsoft as an identity provider using the App Registration created in step 1. Configure it to "Require authentication" and return an HTTP 401 Unauthorized for unauthenticated requests.
- Set Function Auth Level: In your HTTP Trigger definition (
function.jsonor C# attribute), set the authorization level toAnonymous(AuthorizationLevel.Anonymous). This seems counter-intuitive, but it tells the Function runtime to let Easy Auth handle the security rather than looking for a function-level API key. - Access Claims: Your function can now safely assume any incoming request is authenticated. You can access the injected
ClaimsPrincipalto read user data.
Please let me know in comments if any further questions on this.
Note: This response is drafted with the help of AI systems.