Difference between AllowedHosts in appsettings.json and UseCors in .NET Core API 3.x
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
AllowedHosts and UseCors both affect incoming HTTP traffic in ASP.NET Core 3.x, but they solve completely different problems. AllowedHosts is about which Host headers your server will accept, while CORS is about which browser origins are allowed to read responses from your API.
If you treat them as interchangeable, the configuration becomes confusing very quickly. The easiest way to understand the difference is to look at the HTTP headers each one cares about and the layer where it applies.
What AllowedHosts Does
AllowedHosts is configured in appsettings.json and is used to restrict valid request host names. Its main job is to reduce exposure to host-header based misrouting or host-header injection issues.
A typical configuration looks like this:
This setting is about the Host header that comes with the request, for example:
If the application receives a request with an unexpected host name, the host filtering behavior can reject it. This matters at the server boundary, regardless of whether the client is a browser, curl, Postman, or another backend service.
What UseCors Does
CORS stands for Cross-Origin Resource Sharing. It is a browser security mechanism, not a host-header filter. When a frontend running at one origin wants to call an API at another origin, the browser checks whether the API allows that cross-origin access.
In ASP.NET Core 3.x, you usually configure a named CORS policy in Startup:
This configuration is about the browser's Origin header and the API's Access-Control-Allow-Origin response header, not the Host header.
The Two Features Protect Different Boundaries
The distinction becomes clearer if you compare their inputs:
- '
AllowedHostschecks the host name being used to reach your server' - CORS checks which browser origin is trying to read the response
That means:
- '
AllowedHostsapplies to all clients' - CORS is mainly enforced by browsers
If Postman can call your API but browser JavaScript cannot, that points to CORS, not AllowedHosts. If the request is reaching the wrong host name or being rejected because the host header is unexpected, that points to AllowedHosts, not CORS.
A Simple Mental Example
Suppose your API is hosted at https://api.example.com, and your frontend runs at https://app.example.com.
The request might look like this:
- '
Hostheader isapi.example.com' - '
Originheader ishttps://app.example.com'
From the server's perspective:
- '
AllowedHostsdecides whetherapi.example.comis an allowed destination host' - CORS decides whether
https://app.example.comis allowed to call the API from browser code
They answer different questions, so configuring one does not replace the other.
Middleware Order Matters for CORS
In ASP.NET Core 3.x, UseCors should typically run after UseRouting and before UseEndpoints. If you place it incorrectly, the policy may not apply to your controllers as expected.
The common pattern is:
AllowedHosts, by contrast, is driven by host filtering configuration and does not replace this middleware setup.
Common Pitfalls
- Using
AllowedHoststo try to fix a browser CORS error. It does not control cross-origin browser access. - Assuming CORS protects the API from all non-browser clients. Tools such as
curlcan still call the endpoint directly. - Forgetting that
AllowedHostsis about the requestHostheader, not the browserOriginheader. - Placing
UseCorsin the wrong position in the ASP.NET Core pipeline. - Using overly broad CORS policies in development and forgetting to tighten them in production.
Summary
- '
AllowedHostsrestricts accepted request host names at the server level.' - '
UseCorscontrols which browser origins may access your API across origins.' - '
AllowedHostsis about theHostheader; CORS is about theOriginheader and CORS response headers.' - CORS is mainly a browser concern, while host filtering applies more broadly.
- In ASP.NET Core 3.x, configure
UseCorsin the middleware pipeline at the correct point or the policy may not take effect.

