[release/10.0] Preserve antiforgery token#64819
Open
github-actions[bot] wants to merge 3 commits intorelease/10.0from
Open
[release/10.0] Preserve antiforgery token#64819github-actions[bot] wants to merge 3 commits intorelease/10.0from
github-actions[bot] wants to merge 3 commits intorelease/10.0from
Conversation
…HostBuilder.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Contributor
|
Hi @@github-actions[bot]. Please make sure you've updated the PR description to use the Shiproom Template. Also, make sure this PR is not marked as a draft and is ready-to-merge. To learn more about how to prepare a servicing PR click here. |
Member
|
/azp run |
|
Azure Pipelines successfully started running 4 pipeline(s). |
3 tasks
Contributor
|
Looks like this PR hasn't been active for some time and the codebase could have been changed in the meantime. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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