Overview
Cross-Origin Resource Sharing (CORS) allows your frontend application to make HTTP requests to the ApiTickets backend from a different origin. The API is configured to accept requests from a specific frontend URL defined in configuration.CORS Setup in Program.cs
The CORS policy is configured in theProgram.cs file during application startup:
API/Program.cs
Frontend URL Configuration
Development Environment
The allowed frontend origin is configured inappsettings.Development.json:
appsettings.Development.json
The complete URL of your frontend application, including protocol and port (e.g.,
http://localhost:5173 for Vite/React)Production Environment
For production deployments, create or updateappsettings.Production.json with your production frontend URL:
appsettings.Production.json
CORS Policy Details
The configured CORS policy includes:WithOrigins(web)
Only allows requests from the specified frontend URL. This prevents unauthorized domains from accessing your API.AllowAnyMethod()
Permits all HTTP methods:- GET
- POST
- PUT
- PATCH
- DELETE
- OPTIONS
AllowAnyHeader()
Allows all HTTP headers in requests, including:Content-TypeAuthorization- Custom headers
AllowCredentials()
Enables sending credentials (cookies, authorization headers, TLS client certificates) in cross-origin requests. This is required for JWT authentication to work properly.The
AllowCredentials() setting is essential for JWT token authentication. Without it, the Authorization header will not be sent with requests.Configuration Steps
Identify Frontend URL
Determine the full URL where your frontend application runs:
- Vite/React: Usually
http://localhost:5173 - Next.js: Usually
http://localhost:3000 - Angular: Usually
http://localhost:4200
Configure Production Settings
For production, add your production domain to
appsettings.Production.json:Multiple Origins (Advanced)
If you need to allow multiple frontend origins, modify the CORS configuration:Program.cs
Middleware Order
The CORS middleware must be placed before authentication and authorization:Program.cs
If CORS is placed after authentication, OPTIONS preflight requests will fail because they don’t include authentication headers.
Security Considerations
Never Use AllowAnyOrigin with AllowCredentials
Validate Origins Strictly
Use Environment Variables for Production
Testing CORS Configuration
Browser Developer Tools
- Open your frontend application
- Open DevTools (F12)
- Make an API request
- Check the Network tab for CORS headers:
Common CORS Errors
Error: “No ‘Access-Control-Allow-Origin’ header is present”- Solution: Ensure
UseCors()is called in middleware pipeline
- Solution: Replace
AllowAnyOrigin()withWithOrigins(specificUrl)
- Solution: Add
AllowCredentials()to CORS policy
Troubleshooting
CORS Errors Persist After Configuration
- Verify
appsettings.Development.jsonis in the correct directory - Check that
webconfiguration key matches exactly - Restart the API server completely
- Clear browser cache and hard reload (Ctrl+Shift+R)