Remove redundant app.UseAntiforgery() from Blazor Web templates#67119
Open
DeagleGross wants to merge 1 commit into
Open
Remove redundant app.UseAntiforgery() from Blazor Web templates#67119DeagleGross wants to merge 1 commit into
DeagleGross wants to merge 1 commit into
Conversation
Both Program.cs (top-level) and Program.Main.cs variants of the BlazorWeb-CSharp template no longer need the explicit call: CsrfProtectionMiddleware (auto-injected by WebApplication) handles cross-site request protection via Sec-Fetch-Site/Origin, and the Razor Components endpoint invoker no longer self-validates token-based antiforgery when the new CSRF middleware ran. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR removes the explicit app.UseAntiforgery(); call from the Blazor Web C# template’s two Program.cs variants, intending to rely on the newer CSRF middleware behavior instead of the legacy antiforgery middleware.
Changes:
- Removed
app.UseAntiforgery();from the top-level statements templateProgram.cs. - Removed
app.UseAntiforgery();from theMain-style templateProgram.Main.cs.
Show a summary per file
| File | Description |
|---|---|
| src/ProjectTemplates/Web.ProjectTemplates/content/BlazorWeb-CSharp/BlazorWebCSharp.1/Program.cs | Removes UseAntiforgery() from the template pipeline. |
| src/ProjectTemplates/Web.ProjectTemplates/content/BlazorWeb-CSharp/BlazorWebCSharp.1/Program.Main.cs | Removes UseAntiforgery() from the template pipeline (Main-style). |
Copilot's findings
- Files reviewed: 2/2 changed files
- Comments generated: 2
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.
Removes the explicit
app.UseAntiforgery();call from the twoProgram.csvariants of theBlazorWeb-CSharptemplate. The call is now redundant in Blazor apps because:CsrfProtectionMiddleware(auto-injected byWebApplication.CreateBuildersince Implement Cross-Site Request Forgery Algorithm based on Fetch Metadata headers #66585) blocks cross-site state-changing requests viaSec-Fetch-Site/Originheaders — covering the protection thatUseAntiforgery()was previously providing for Blazor form posts in the template.UseAntiforgery()is internally consistent.Files changed
src/ProjectTemplates/Web.ProjectTemplates/content/BlazorWeb-CSharp/BlazorWebCSharp.1/Program.cssrc/ProjectTemplates/Web.ProjectTemplates/content/BlazorWeb-CSharp/BlazorWebCSharp.1/Program.Main.csMain-styleEach file: 2 deletions (the
app.UseAntiforgery();line plus the now-orphan blank line below it).Scope notes
A full-tree
grep "UseAntiforgery"confirmed these are the only templates that call the method. No MVC, Razor Pages, Web API, or other Web templates touch antiforgery. Test assets and framework source intentionally left alone.grep "Antiforgery" src/ProjectTemplates/test/returns zero matches — no snapshot/baseline test needs updating.Dependency
EndpointMiddlewarestill requires the legacy AF middleware to have run.Verification
dotnet build src/ProjectTemplates/Web.ProjectTemplates/Microsoft.DotNet.Web.ProjectTemplates.csproj→ 0 errors / 0 warnings.src/ProjectTemplatesconfirms noUseAntiforgeryreferences remain in template content.Closes #67084