Some troubleshooting tips, after I managed to waste two hours on the most trivial CORS issue:
If you see CORS policy execution failed
logged... Don't assume that your CORS policy is not executing properly. In fact, the CORS middleware works, and your policy is executing properly. The only thing this badly worded message means is that the request's origin doesn't match any of the allowed origins (see source), i.e. the request is disallowed.
The origin check (as of ASP.NET Core 5.0) happens in a very simple way... i.e. case-sensitive ordinal string comparison (see source) between the strings you provided via WithOrigins()
and what exists in HttpContext.Request.Headers[Origin]
.
CORS can fail if you set an allowed origin with a trailing slash /
, or if it contains uppercase letters. (In my case I did in fact accidentally copy the host with a trailing slash.)