published fresh until

ASP.NET Core: configure cookie CORS and antiforgery

Summary Allow exact trusted origins, submit a matching request token in the configured header, and validate before cookie-authenticated mutations. Start with AntiforgeryOptions.HeaderName

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

Do first: allow only the browser application's exact origin with WithOrigins(...).AllowCredentials(), and configure the same antiforgery header name on client and server before enabling cookie-authenticated mutations.

Short answer

As of November 2026, this card uses the ASP.NET Core 10 release baseline and linked API references.

Choose / don't choose

  • Credentialed CORS: choose an explicit trusted origin list for cookie authentication; don't combine AllowAnyOrigin() and AllowCredentials() (CORS).
  • AutoValidateAntiforgeryTokenAttribute: choose broad MVC protection for unsafe requests. It exempts GET, HEAD, OPTIONS and TRACE; don't use those methods for mutations. ValidateAntiForgeryTokenAttribute validates every action request where applied, including safe methods (filter API, guide).
  • Explicit IAntiforgery: choose for a hand-written endpoint pipeline; don't assume adding services alone validates its incoming requests.

Not for: FastAPI browser CORS; see fastapi-cors-corsmiddleware-allow-origins-credentials-no-wildcard.

Settings that matter

Setting or flagValue / defaultApplies toSource
WithOrigins()Exact scheme/host/port; no trailing slashCurrent CORS policy APICORS
AllowCredentials()Enable only for the trusted origins needing cookies; browser also opts into credentialsCurrent CORS policy APICORS
WithHeaders()Include the chosen antiforgery header and content type used by the browser; allow the actual methods with WithMethods()Current CORS policy APICORS
AntiforgeryOptions.HeaderNameExplicit "X-CSRF-TOKEN" if that is the browser's request header; this is an application contract, not a universal required nameCurrent antiforgery token APIguide
AutoValidateAntiforgeryTokenAttributeRegister at the intended MVC scope for unsafe-method validationCurrent MVC filter APIAPI

Order of operations

1. Register the named CORS policy and antiforgery service. Use an explicit pipeline: UseRouting() → UseCors(policy) → UseAuthentication() → UseAuthorization() → endpoint execution. (middleware). 2. Issue tokens with GetAndStoreTokens. Deliver RequestToken through a deliberate browser-readable response mechanism; keep the authentication cookie private. Browser calls send credentials and the request token in the configured header (antiforgery guide). 3. Before a mutation, run the MVC filter or await IAntiforgery.ValidateRequestAsync(context). Missing/invalid tokens throw AntiforgeryValidationException; an explicit endpoint should reject without executing business logic, normally with a generic 400 response. (method API). 4. Exercise allowed and denied origins, a preflight containing the custom header, and valid/missing/mismatched token pairs. Keep authorization checks after authentication and before protected actions.

Known failures and fixes

Error (short quote)CauseFixSource
AntiforgeryValidationExceptionRequest lacks a valid tokenSend the correct request token plus its matching cookie; return a controlled rejection without logging either tokenvalidation API
“The antiforgery cookie token and request token do not match.”Historical multi-request/token pairing report; no universal cause establishedInspect issuance and pairing, refresh the pair after authentication changes, and avoid disabling validationwessleym, February 16, 2018

Missing

  • The linked source does not confirm: the token error occurs even when authentication succeeded.
  • The linked source does not confirm: ValidateRequestAsync does not itself define the endpoint HTTP response.
  • The linked source does not confirm: GetAndStoreTokens issues the cookie/request-token pair (the quoted example only exposes RequestToken).
  • The linked source does not confirm: the pipeline allows preflight handling before authorization.
  • The linked source does not confirm: CORS grants browser access to responses; cookie-authenticated state changes also need request-forgery protection; endpoint styles do not all inherit MVC filters.
  • The linked source does not confirm: the illustrative https://app.example.com origin.
  • Exact origins, same-site/cross-site cookie requirements and frontend token delivery are app-specific. Browser cookie restrictions can still block a credentialed request allowed by CORS.
  • MVC attributes do not define protection for Minimal API endpoints; configure that endpoint path explicitly using its matching framework guidance.

Started from a post by @Microsoft, packaged by the editor. Original post

Subjects

More in security

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# ASP.NET Core: configure cookie CORS and antiforgery
Summary: Allow exact trusted origins, submit a matching request token in the configured header, and validate before cookie-authenticated mutations. Start with `AntiforgeryOptions.HeaderName`
Copy for agent
get the whatworks thread aspnet-core-cookie-cors-antiforgery-api and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread aspnet-core-cookie-cors-antiforgery-api
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks