AOT-Compatible Web API Authentication for .NET 10+#3705
Conversation
jennyf19
left a comment
There was a problem hiding this comment.
Will there be tests added? and maybe yes to dropping b2c in the AOT path
Yes tests are next. I first want to test I this does not break the current behavior at all (as I've factorized code) Dropping b2c ... what I asked this afternoon ... |
Added comprehensive unit tests in commit fcac19b (
Tests will run once Microsoft.Identity.Abstractions 11.0.0 is published or built locally. |
There was a problem hiding this comment.
Pull request overview
This PR adds AOT-compatible Web API authentication methods for .NET 10+ that use MicrosoftIdentityApplicationOptions instead of the reflection-heavy MicrosoftIdentityOptions. The implementation introduces new extension methods AddMicrosoftIdentityWebApiAot(), shared helper utilities, and a post-configurator for validation and OBO support.
Changes:
- Added new AOT-compatible extension methods for Web API authentication (NET10_0_OR_GREATER only)
- Introduced shared helper methods in
IdentityOptionsHelpersfor authority building, validation, and token storage - Created a post-configurator for JWT Bearer options that validates and configures authentication after all options are merged
- Modified existing code to use shared helpers and added AOT-compatible configuration binding for .NET 10+
Reviewed changes
Copilot reviewed 20 out of 20 changed files in this pull request and generated 8 comments.
Show a summary per file
| File | Description |
|---|---|
tests/Microsoft.Identity.Web.Test/WebApiExtensionsAotTests.cs |
Comprehensive unit tests for AOT extension methods covering service registration, configuration, validation, and OBO support |
tests/Microsoft.Identity.Web.AotCompatibility.TestApp/Program.cs |
Test application demonstrating AOT-compatible API usage with WebApplication.CreateSlimBuilder |
tests/Microsoft.Identity.Web.AotCompatibility.TestApp/Microsoft.Identity.Web.AotCompatibility.TestApp.csproj |
Updated target framework to net10.0 with PublishAot enabled |
src/Microsoft.Identity.Web/WebApiExtensions/MicrosoftIdentityWebApiAuthenticationBuilderExtensions.cs |
Made class partial to support AOT extension file |
src/Microsoft.Identity.Web/WebApiExtensions/MicrosoftIdentityWebApiAuthenticationBuilderExtensions.Aot.cs |
New AOT-compatible extension methods with configuration section and programmatic overloads |
src/Microsoft.Identity.Web/WebApiExtensions/MicrosoftIdentityWebApiAuthenticationBuilder.cs |
Refactored to use shared token storage helper |
src/Microsoft.Identity.Web/PublicAPI/net*.0/InternalAPI.Unshipped.txt |
Added internal API entries for IdentityOptionsHelpers and PostConfigurator across all target frameworks |
src/Microsoft.Identity.Web/PublicAPI/net10.0/PublicAPI.Unshipped.txt |
Added public API entries for the two AddMicrosoftIdentityWebApiAot overloads |
src/Microsoft.Identity.Web/PostConfigureOptions/MicrosoftIdentityJwtBearerOptionsPostConfigurator.cs |
New post-configurator that validates options and configures JWT Bearer authentication (validation, authority, audience, issuer, token decryption, OBO) |
src/Microsoft.Identity.Web/Internal/IdentityOptionsHelpers.cs |
Shared helper methods for authority building, validation, audience configuration, and token storage chaining |
src/Microsoft.Identity.Web.TokenAcquisition/WebApiBuilders.cs |
Updated to use MicrosoftIdentityOptionsBinder for NET10_0_OR_GREATER |
src/Microsoft.Identity.Web.TokenAcquisition/PublicAPI/net*.0/InternalAPI.Unshipped.txt |
Added MicrosoftIdentityOptionsBinder to internal API surface |
src/Microsoft.Identity.Web.TokenAcquisition/Microsoft.Identity.Web.TokenAcquisition.csproj |
Updated IsAotCompatible and EnableConfigurationBindingGenerator to net10.0 |
src/Microsoft.Identity.Web.TokenAcquisition/Internal/MicrosoftIdentityOptionsBinder.cs |
New AOT-compatible configuration binder for MicrosoftIdentityOptions (manually binds properties without reflection) |
AOT-Compatible Web API Authentication for .NET 10+
Summary
Adds
AddMicrosoftIdentityWebApiAot()- an AOT-compatible alternative toAddMicrosoftIdentityWebApi()that usesMicrosoftIdentityApplicationOptions(Abstractions 11) instead of the reflection-heavyMicrosoftIdentityOptions. OBO works automatically with justAddMicrosoftIdentityWebApiAot()+AddTokenAcquisition().New API (
NET10_0_OR_GREATERonly)Example 1 - Source-generated configuration binding
The caller passes
azureAdSection.Bind(options)as the configure delegate. With the<EnableConfigurationBindingGenerator>true</EnableConfigurationBindingGenerator>MSBuild property in the project file, the .NET source generator produces compile-time binding code for thatBind()call — no reflection at runtime, fully AOT-safe.Example 2 - Programmatic configuration
Key Design Decisions
IdentityOptionsHelpers) - common logic (issuer/audience validation, token storage) used by both AOT and non-AOT pathsFiles
Internal/IdentityOptionsHelpers.csWebApiExtensions/...BuilderExtensions.Aot.csAddMicrosoftIdentityWebApiAot()extension methodsPostConfigureOptions/...PostConfigurator.csWebApiExtensionsAotTests.csTesting