Skip to content

Add Microsoft Entra ID service-to-service authentication and resilient HTTP clients - #205

Open
gentilegianluca wants to merge 2 commits into
marcominerva:developfrom
gentilegianluca:feat/entra-id-microservices
Open

gentilegianluca wants to merge 2 commits into
marcominerva:developfrom
gentilegianluca:feat/entra-id-microservices

Conversation

@gentilegianluca

Copy link
Copy Markdown

Implements #203 and #204.

Summary

Adds the optional SimpleAuthenticationTools.EntraId package (src/SimpleAuthentication.EntraId, net8.0–net11.0), built on Microsoft.Identity.Web and Microsoft.Extensions.Http.Resilience, for service-to-service authentication with Microsoft Entra ID. It works next to the schemes and policies already registered by the application and doesn't change their defaults.

What's included

Authentication (#203)

  • AddEntraIdJwtBearer: named JWT Bearer scheme (EntraId by default, configurable) for a single tenant. TenantId and ClientId must be GUIDs; signature, issuer, audience and lifetime are always validated, even if the configuration tries to disable them; tokens of other tenants are rejected. The configuration is validated at startup.
  • The registration doesn't change the default scheme and doesn't add a fallback policy. If the application has a single scheme, which ASP.NET Core uses automatically as the default one, that scheme remains the default one after adding Entra ID.

Authorization (#203)

  • RequireEntraIdApplicationRole (policy builder and endpoint convention): accepts only app-only tokens (idtyp = app, no scp) validated by the Entra ID scheme and containing one of the required application roles. Claims coming from other schemes can't satisfy the requirement.

HTTP clients (#204)

  • AddEntraIdHttpClient: named client (usable as typed client, also with Refit) that acquires app-only tokens through Microsoft.Identity.Web, using the credentials and the token cache of the host. The token of the incoming request is never forwarded.
  • Each client is bound to its HTTPS BaseAddress (HTTP only on loopback, with explicit opt-in). Requests to other destinations and Authorization/Host headers set by the caller are rejected; redirects and cookies are disabled; the token is redacted from logs.
  • Standard resilience pipeline (timeouts, retries, circuit breaker), configurable from configuration or code. Only safe methods are retried; unsafe ones can be retried per request with MarkAsIdempotent(). A single pipeline per client: handlers from ConfigureHttpClientDefaults are removed, and adding or removing resilience handlers afterwards is reported when the client is created.

Sample, tests and docs

  • samples/MinimalApis/EntraIdMicroservices: Orders API calling an Inventory API with its own identity.
  • tests/SimpleAuthentication.EntraId.Tests: valid and invalid tokens (signature, audience, issuer, tenant, lifetime), application roles, coexistence with other schemes, HTTP client security and resilience, and an end-to-end call between two APIs with the real Microsoft.Identity.Web/MSAL stack against a simulated Entra ID authority.
  • Package README with Entra ID setup, credentials, migration from API keys, token verification and troubleshooting; new section in the main README.
  • Solution, .gitignore, version.json and a publish_entraid.yml workflow aligned with the existing packages.

Testing

  • 158 tests on each target framework (net8.0, net9.0, net10.0, net11.0), all passing in Debug and Release.
  • The test project uses xunit.v3.mtp-off: with the .NET 10+ SDK, dotnet test doesn't run Microsoft Testing Platform projects without an opt-in in global.json.
  • Verified end to end with two real services in an Entra ID tenant (application role, idtyp optional claim, admin consent, client credentials), including the coexistence with API keys described in the README.

Notes

  • publish_entraid.yml needs a NuGet trusted publishing policy for this workflow file before the first release.
  • The repository has no workflow that builds and runs tests, so the new tests don't run in CI yet.
  • User-delegated scenarios (On-Behalf-Of) are out of scope, as stated in Add Microsoft Entra ID support for service-to-service authentication #203.
  • The package requires the latest patch of Microsoft.AspNetCore.Authentication.JwtBearer for each target framework, like SimpleAuthenticationTools.Abstractions: applications referencing an older patch directly get NU1605 and need to update it.

Gianluca Gentile added 2 commits November 18, 2026 08:07
…va#204)

Add a complete guide to the package README: quick start, how token validation works, Microsoft Entra ID setup for the target API and the client, credentials, migration from API keys, token verification, troubleshooting, requirements and limitations. Link the sample README to the new sections.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant