-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Preserve antiforgery token #64806
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Preserve antiforgery token #64806
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR fixes a critical trimming issue in Blazor WebAssembly applications with Individual Identity authentication. When published with PublishTrimmed=true, the antiforgery token state was being lost during the SSR-to-WASM handoff, causing form submissions to break. The fix adds DynamicDependency attributes to preserve the necessary types from IL trimming.
Key changes:
- Added
[DynamicDependency(JsonSerialized, typeof(DefaultAntiforgeryStateProvider))]to preserve theCurrentTokenproperty marked with[PersistentState] - Added
[DynamicDependency(JsonSerialized, typeof(AntiforgeryRequestToken))]to preserve the constructor and properties for JSON deserialization - These attributes are placed on
InitializeDefaultServices()method, following the established pattern used elsewhere in the codebase
src/Components/WebAssembly/WebAssembly/src/Hosting/WebAssemblyHostBuilder.cs
Outdated
Show resolved
Hide resolved
…HostBuilder.cs Co-authored-by: Copilot <[email protected]>
|
Let's make sure we update our manual tests to add coverage for this in the future. |
|
/backport to release/10.0 |
|
Started backporting to |
Fix antiforgery token trimming in Blazor WebAssembly prerendering
Add DynamicDependency attributes to prevent IL trimming of antiforgery type.
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 deserializationKnown workarounds:
Fixes #64693