A CORS preflight (OPTIONS with Origin and Access-Control-Request-Method) answered HTTP/1.1 401 Unauthorized had one cause in this test: authorization ran before CORS. The app had a fallback policy that requires a signed-in user and called app.UseCors() after app.UseAuthorization(). Moving UseCors above UseAuthentication and UseAuthorization made the same preflight return 204 No Content with the CORS headers.

The real request after the preflight still needs its token: after the change, a PUT without one still got 401, now with the CORS header on it.

The error

The preflight and its response, then the server console line at Information level (the test's appsettings.json sets "Microsoft.AspNetCore.Authorization": "Information" and "Microsoft.AspNetCore.Cors": "Information"):

PS> curl.exe -s -i -X OPTIONS -H "Origin: http://localhost:5173" -H "Access-Control-Request-Method: PUT" http://127.0.0.1:15630/patients/7
HTTP/1.1 401 Unauthorized
Content-Length: 0
Date: Sat, 14 Nov 2026 01:26:57 GMT
Server: Kestrel
WWW-Authenticate: Bearer
...
info: Microsoft.AspNetCore.Authorization.DefaultAuthorizationService[2]
      Authorization failed. These requirements were not met:
      DenyAnonymousAuthorizationRequirement: Requires an authenticated user.

No CORS line was logged for this request and the response has no Access-Control-Allow-Origin or Access-Control-Allow-Methods.

Why it happens

The pipeline that produced it:

...
builder.Services.AddAuthorization(options =>
{
    options.FallbackPolicy = new AuthorizationPolicyBuilder().RequireAuthenticatedUser().Build();
});
builder.Services.AddCors(options =>
{
    options.AddPolicy("Frontend", policy => policy
        .WithOrigins("http://localhost:5173")
        .AllowAnyHeader()
        .AllowAnyMethod());
});

var app = builder.Build();

app.UseAuthentication();
app.UseAuthorization();
app.UseCors("Frontend");

app.MapPut("/patients/{id}", (int id) => $"Updated patient {id}");
...

With UseCors after UseAuthorization, the fallback policy turned the preflight away with 401 and the log held only the authorization line, no CORS line. With UseCors first, the PUT below logged the CORS line before the authorization line. The preflight in this test carried only the Origin and Access-Control-Request-Method headers, as the command shows, so the fallback policy had no user to accept.

The fix

...
app.UseCors("Frontend");
app.UseAuthentication();
app.UseAuthorization();
...
PS> curl.exe -s -i -X OPTIONS -H "Origin: http://localhost:5173" -H "Access-Control-Request-Method: PUT" http://127.0.0.1:15630/patients/7
HTTP/1.1 204 No Content
Date: Sat, 14 Nov 2026 01:26:59 GMT
Server: Kestrel
Access-Control-Allow-Methods: PUT
Access-Control-Allow-Origin: http://localhost:5173

The log now held CORS policy execution successful. and no authorization line for the preflight. Authorization was not weakened by the move, as the PUT without a token shows:

PS> curl.exe -s -i -X PUT -H "Origin: http://localhost:5173" http://127.0.0.1:15630/patients/7
HTTP/1.1 401 Unauthorized
Content-Length: 0
Date: Sat, 14 Nov 2026 01:27:41 GMT
Server: Kestrel
Access-Control-Allow-Origin: http://localhost:5173
WWW-Authenticate: Bearer

Compare the two 401 responses: before the move the 401 went to the preflight and had no CORS header; after it, the 401 went to the PUT itself and carried Access-Control-Allow-Origin.

How it was reproduced

A minimal API made with dotnet new web -f net10.0 on SDK 11.0.100, with Microsoft.AspNetCore.Authentication.JwtBearer 10.0.0, running on Microsoft.AspNetCore.App 10.0.15 in the Development environment, built and started by a small script, start.ps1, which runs dotnet build -nologo -v q in the project folder and then bin\Debug\net10.0\FixLab.exe --urls http://127.0.0.1:15630. The preflight was imitated with curl.exe -X OPTIONS and the two headers of a minimal preflight; no browser was used. FixLab, port 15630, the route /patients/{id}, the policy name Frontend and the origin http://localhost:5173 are the test's own.

Frequently asked

Why does my CORS preflight OPTIONS request return 401 in ASP.NET Core?
In this test, because authorization ran before CORS: a fallback policy required a signed-in user and UseCors came after UseAuthorization, so the preflight, which has no token, got 401 without CORS headers.
Where should UseCors go in the ASP.NET Core pipeline?
Before UseAuthentication and UseAuthorization. In the test that order made the preflight return 204 with Access-Control-Allow-Origin and Access-Control-Allow-Methods.
Does moving UseCors first let requests skip authorization?
Not in the test. After the move a PUT without a token still got 401, and the response then carried the Access-Control-Allow-Origin header.

More decoded errors in the Fixes category. The log line in the error block has its own post: 'DenyAnonymousAuthorizationRequirement: Requires an authenticated user'.