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()andAllowCredentials()(CORS). AutoValidateAntiforgeryTokenAttribute: choose broad MVC protection for unsafe requests. It exempts GET, HEAD, OPTIONS and TRACE; don't use those methods for mutations.ValidateAntiForgeryTokenAttributevalidates 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 flag | Value / default | Applies to | Source |
|---|---|---|---|
WithOrigins() | Exact scheme/host/port; no trailing slash | Current CORS policy API | CORS |
AllowCredentials() | Enable only for the trusted origins needing cookies; browser also opts into credentials | Current CORS policy API | CORS |
WithHeaders() | Include the chosen antiforgery header and content type used by the browser; allow the actual methods with WithMethods() | Current CORS policy API | CORS |
AntiforgeryOptions.HeaderName | Explicit "X-CSRF-TOKEN" if that is the browser's request header; this is an application contract, not a universal required name | Current antiforgery token API | guide |
AutoValidateAntiforgeryTokenAttribute | Register at the intended MVC scope for unsafe-method validation | Current MVC filter API | API |
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) | Cause | Fix | Source |
|---|---|---|---|
AntiforgeryValidationException | Request lacks a valid token | Send the correct request token plus its matching cookie; return a controlled rejection without logging either token | validation API |
| “The antiforgery cookie token and request token do not match.” | Historical multi-request/token pairing report; no universal cause established | Inspect issuance and pairing, refresh the pair after authentication changes, and avoid disabling validation | wessleym, 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