Ravi Vishwakarma is a dedicated Software Developer with a passion for crafting efficient and innovative solutions. With a keen eye for detail and years of experience, he excels in developing robust software systems that meet client needs. His expertise spans across multiple programming languages and technologies, making him a valuable asset in any software development project.
ICSM Computer
10-Jun-2025CORS (Cross-Origin Resource Sharing) is a browser security mechanism that controls whether a web page from one origin can make requests to a different origin.
What is an "Origin"?
An origin is defined by:
Example:
https://api.example.com≠https://www.example.comhttp://localhost:5000≠http://localhost:5001Why Does CORS Exist?
To protect users from cross-origin attacks, like a malicious script on
evil.commaking unauthorized API calls tobank.com.Browsers block such requests by default unless the server explicitly allows them using CORS headers.
What Happens in a CORS Request?
When JavaScript tries to call an API on another origin:
OPTIONS.If not present → browser blocks the request (but the request still hits the server).
How CORS Relates to Authentication
CORS doesn’t provide authentication, but it impacts whether a frontend can call an authenticated API:
1. APIs using cookies (session-based)
To allow cross-origin requests with credentials:
Cannot use
*forAccess-Control-Allow-Originif using credentials.2. APIs using tokens (e.g., JWT)
Frontend adds
Authorizationheader:Server must allow the
Authorizationheader in:No need for cookies or
Access-Control-Allow-Credentials.Summary: CORS vs. Authentication
AuthorizationheaderIn ASP.NET Framework (Web API):
You can enable CORS using NuGet:
Then configure: