[release/10.0] Preserve antiforgery token #64819
Open
+2
−0
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Backport of #64806 to release/10.0
/cc @javiercn @ilonatommy
Preserve antiforgery token
Fix antiforgery token trimming in Blazor WebAssembly prerendering
Description
When a Blazor WebAssembly app with Individual Identity authentication is published with
PublishTrimmed=true, the antiforgery token persisted during SSR is not restored during the SSR-to-WASM handoff. This causes the<AntiforgeryToken>component to render nothing in interactive mode, breaking form submissions.Root cause: The IL trimmer removes
DefaultAntiforgeryStateProvider.CurrentTokenproperty andAntiforgeryRequestTokenconstructor because they're only accessed via reflection by the persistent state system.Fix: Add
[DynamicDependency(JsonSerialized, typeof(...))]attributes onWebAssemblyHostBuilder.InitializeDefaultServices()to preserve:DefaultAntiforgeryStateProvider- ensures the[PersistentState] CurrentTokenproperty is preservedAntiforgeryRequestToken- ensures the constructor and properties are preserved for JSON deserializationFixes #64693
Customer Impact
Customers publishing Blazor WebAssembly apps with Individual Identity authentication experience is broken form submissions because the antiforgery token is not properly restored after SSR-to-WASM handoff. This is a critical issue affecting production deployments with trimming enabled.
Regression?
Regressed from .NET 9. In .NET 9, the antiforgery token was retrieved using
PersistentComponentState. In .NET 10, it was changed to use the[PersistentState]attribute onDefaultAntiforgeryStateProvider.CurrentToken. The new declarative model for persisting state causes the IL trimmer to remove the necessary types since they're only accessed via reflection.Risk
The fix adds
DynamicDependencyattributes to preserve specific types from IL trimming. This is a minimal, targeted change following established patterns already used elsewhere in the codebase. It only affects the trimmer behavior and has no runtime logic changes.Verification
Manual testing with a Blazor WebAssembly app using Individual Identity authentication to verify form submissions work correctly.
Packaging changes reviewed?
No packaging changes in this PR.
Existing workarounds