ASP.NET Core Minimal API File Uploads and Antiforgery
ASP.NET Core · Published · CoreUpload team
A Minimal API can accept a file in a few lines. Treat file binding, antiforgery and authorization as separate concerns so a convenient endpoint does not become a confusing or unsafe production integration.
Use multipart form binding for IFormFile
For a form upload, the file parameter name is part of the HTTP contract. A handler parameter named file expects a multipart part with that name. A browser FormData body is appropriate; a JSON object containing a File is not. Let the browser generate the Content-Type boundary instead of setting multipart/form-data manually.
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddAntiforgery();
var app = builder.Build();
app.UseAntiforgery();
app.MapPost("/inspect-upload", (IFormFile file) =>
file.Length == 0
? Results.BadRequest(new { error = "Choose a non-empty file." })
: Results.Ok(new { receivedBytes = file.Length }));
app.Run();This small application inspects the bound file; it does not save it or provide authentication. The request must still provide the antiforgery cookie/token pair. In .NET 8 and later, form-bound Minimal API file endpoints participate in the antiforgery requirements; registration alone does not mean a caller already has a valid token.
Obtain the token from the same application
A Razor form can render an antiforgery field. A JavaScript client needs a documented way to obtain the token and send it alongside the matching cookie. Configure a request header name if using a header, and keep the token endpoint and upload endpoint on the intended origin. Treat an expired page or missing cookie as an explainable failure, not as a successful upload.
In an app that uses authentication and authorization middleware, arrange the pipeline so those checks run before antiforgery middleware. Use the framework guidance for your target version. Do not solve a missing-middleware error by disabling request protection on every endpoint. An explicit bearer-token-only API has a different security design from a cookie-authenticated browser form.
Keep CoreUpload endpoint rules explicit
CoreUpload can map its upload API as a route group. Apply your authorization policy to that group and configure upload-specific antiforgery behavior for the installed package version. A native Minimal API parameter-binding example does not automatically describe every custom endpoint that reads Request.Form itself.
// In an application with authentication already configured:
app.MapCoreUploadEndpoints().RequireAuthorization();The authorization policy must also cover per-file operations such as information, download and deletion. If the application uses its own attachment ownership rules, enforce them server-side; possession of an upload identifier should not silently become a cross-tenant authorization rule.
Diagnose the failed boundary
- Wrong form field name: inspect the multipart part names and the handler parameter.
- Missing antiforgery services or middleware: check application startup and endpoint metadata.
- Invalid token or missing cookie: inspect the HTTP response and client credentials behavior.
- Unsupported request content type: send a real multipart body.
- Request too large: investigate the proxy and host limits before changing application validation.
Keep API errors machine-readable and handle non-success responses before parsing the success shape. A 400 response needs a user-facing explanation and an appropriate retry path. Record a correlation identifier in diagnostics without writing tokens or file contents into logs.
Test more than a happy-path POST
Exercise a valid token, no token, a wrong token, a missing file and an oversized request. Also test the final endpoint behind the same authentication and reverse proxy arrangement used by the browser. A request that only worked after protection was disabled is not evidence that the intended integration works.
Common questions
Does adding AddAntiforgery send a token to my browser?
No. The client still needs a generated request token and the associated cookie, then must return them according to the application's configuration.
Does CORS replace antiforgery?
No. CORS controls which cross-origin browser scripts can access responses; it is not a substitute for the request protections required by your authentication model.
Related guides and working examples
Official references
Examples describe the stated framework and application workflow. Check your installed component version and hosting configuration before adapting them.