Skip to content

Remove redundant app.UseAntiforgery() from Blazor Web templates#67119

Open
DeagleGross wants to merge 1 commit into
dotnet:mainfrom
DeagleGross:deaglegross/remove-useantiforgery-from-templates
Open

Remove redundant app.UseAntiforgery() from Blazor Web templates#67119
DeagleGross wants to merge 1 commit into
dotnet:mainfrom
DeagleGross:deaglegross/remove-useantiforgery-from-templates

Conversation

@DeagleGross

@DeagleGross DeagleGross commented Jun 10, 2026

Copy link
Copy Markdown
Member

Removes the explicit app.UseAntiforgery(); call from the two Program.cs variants of the BlazorWeb-CSharp template. The call is now redundant in Blazor apps because:

  1. CsrfProtectionMiddleware (auto-injected by WebApplication.CreateBuilder since Implement Cross-Site Request Forgery Algorithm based on Fetch Metadata headers #66585) blocks cross-site state-changing requests via Sec-Fetch-Site / Origin headers — covering the protection that UseAntiforgery() was previously providing for Blazor form posts in the template.
  2. The Razor Components endpoint invoker no longer self-validates token-based antiforgery when the new CSRF middleware ran — companion change in [breaking] Razor Components: defer to upstream antiforgery / CSRF middleware #67082. The token-generation side is also gated on the legacy AF middleware actually running, so omitting UseAntiforgery() is internally consistent.

Files changed

File Variant
src/ProjectTemplates/Web.ProjectTemplates/content/BlazorWeb-CSharp/BlazorWebCSharp.1/Program.cs Top-level statements
src/ProjectTemplates/Web.ProjectTemplates/content/BlazorWeb-CSharp/BlazorWebCSharp.1/Program.Main.cs Main-style

Each 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

⚠️ Depends on #67082 (the Razor Components endpoint invoker noop work that makes this safe). Should merge after that PR. Without #67082, generated Blazor apps would throw on first request because EndpointMiddleware still requires the legacy AF middleware to have run.

Verification

  • dotnet build src/ProjectTemplates/Web.ProjectTemplates/Microsoft.DotNet.Web.ProjectTemplates.csproj → 0 errors / 0 warnings.
  • Re-grep across src/ProjectTemplates confirms no UseAntiforgery references remain in template content.

Closes #67084

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>
Copilot AI review requested due to automatic review settings June 10, 2026 10:48
@github-actions github-actions Bot added the area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates label Jun 10, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 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 template Program.cs.
  • Removed app.UseAntiforgery(); from the Main-style template Program.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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Rework Antiforgery usage in aspnetcore template

2 participants