Endpoint HTTP: POST /book contains anti-forgery metadata, but a middleware was not found that supports anti-forgery. means a minimal API endpoint binds a form, so it asks for antiforgery validation, and the pipeline has no app.UseAntiforgery(). Add that line after UseAuthentication and UseAuthorization.

The message carries its own fix in its second sentence. In the test app the form post went from 500 to 200 with the one line in place and the same request.

The error

PS> curl.exe -s -i -b jar.txt -d "@body.txt" -H "Accept: text/plain" http://127.0.0.1:15610/book | Select-Object -First 14
HTTP/1.1 500 Internal Server Error
Content-Type: text/plain; charset=utf-8
...

System.InvalidOperationException: Endpoint HTTP: POST /book contains anti-forgery metadata, but a middleware was not found that supports anti-forgery.
Configure your application startup by adding app.UseAntiforgery() in the application startup code. If there are calls to app.UseRouting() and app.UseEndpoints(...), the call to app.UseAntiforgery() must go between them. Calls to app.UseAntiforgery() must be placed after calls to app.UseAuthentication() and app.UseAuthorization().
   at Microsoft.AspNetCore.Routing.EndpointMiddleware.ThrowMissingAntiforgeryMiddlewareException(Endpoint endpoint)
   at Microsoft.AspNetCore.Routing.EndpointMiddleware.Invoke(HttpContext httpContext)
...

This is the response body in the Development environment; the request sent Accept: text/plain and the body was this text. The server console logged it under fail: Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware[1]. The console lines before that request were the normal start-up lines (Now listening on, Application started), and the exception came with the first POST to the endpoint.

Why it happens

The endpoint was app.MapPost("/book", ([FromForm] string name) => ...). It binds [FromForm] string name, and the exception says that endpoint carries antiforgery metadata; the GET /form endpoint, which binds no form, kept working in the same run. The test app had AddAntiforgery() in its services and a working GET /form, but the line app.UseAntiforgery() was switched off by a test setting, so the POST reached the endpoint with no antiforgery middleware before it, and the endpoint middleware refused to run it.

The fix

The lab turns the line on and off with a setting; the real lines are these, and the fixed run started without --SkipAntiforgery true, so the line ran:

...
app.UseAuthentication();
app.UseAuthorization();
if (builder.Configuration["SkipAntiforgery"] != "true")
{
    app.UseAntiforgery();
}
...
PS> curl.exe -s -i -b jar.txt -d "@body.txt" -H "Accept: text/plain" http://127.0.0.1:15610/book
HTTP/1.1 200 OK
...

Booked for Maria Garcia.

With app.UseAntiforgery() after authentication and authorization, a fresh form fetched with the cookie jar posted with 200. The order follows the message's own third sentence. The /book route and the form field are the test's own; in your app the message names your endpoint.

How it was reproduced

A project from dotnet new web -n AfLab -f net10.0 on .NET SDK 11.0.100, no NuGet packages, running on ASP.NET Core 10.0.15 in Development on 127.0.0.1:15610: a GET /form endpoint that writes the hidden field from IAntiforgery.GetAndStoreTokens and a POST /book endpoint with [FromForm] string name. A command-line setting (--SkipAntiforgery true) skipped the app.UseAntiforgery() line for the failing run. Requests went through curl 8.22.0 with a cookie jar and the form body in a file.

Frequently asked

Why does a minimal API endpoint say it contains anti-forgery metadata?
In the test the endpoint bound [FromForm] string name, and the exception said that endpoint carried antiforgery metadata, while a GET endpoint without a form parameter kept working. Without app.UseAntiforgery() in the pipeline, the first POST to it fails with a 500.
Where does app.UseAntiforgery() go in Program.cs?
After app.UseAuthentication() and app.UseAuthorization(), and between UseRouting and UseEndpoints if the app calls those. The exception message itself says so.
Does the missing UseAntiforgery error appear at startup?
Not in the test. The app started and served the GET page normally; the exception came with the first POST to the endpoint that binds the form.

More decoded errors in the Fixes category. The same kind of message for authorization is covered in a middleware was not found that supports authorization.