Intent.AspNetCore.Cors module provides a standard CORS (Cross-Origin Resource Sharing) policy for ASP.NET Core applications, enabling your Web API to accept requests from different origins such as web browsers and mobile applications.
Overview
CORS is a security feature implemented by web browsers that restricts web pages from making requests to a different domain than the one serving the web page. This module configures CORS policies to allow controlled cross-origin access to your API.What Gets Generated
CorsConfiguration
Configures CORS policies for your application:Key Features
Origin Control
Allow specific origins or all origins
Method Control
Specify allowed HTTP methods
Header Control
Configure allowed request headers
Credentials
Control cookie and credential sharing
Configuration Options
Allow All Origins (Development)
For development environments:Specific Origins (Production)
For production environments:Named Policies
Define multiple CORS policies:Configuration-Based Origins
Load allowed origins from appsettings.json:CORS Request Flow
Simple Request
For simple requests, the browser includes anOrigin header:
Preflight Request
For complex requests (e.g., with custom headers), the browser sends a preflight OPTIONS request:Advanced Configuration
Allow Credentials
Enable cookies and authentication headers:Expose Headers
Allow client to read custom response headers:Preflight Max Age
Cache preflight responses:Specific Methods and Headers
Restrict to specific methods and headers:Per-Controller CORS
Apply CORS to specific controllers:Environment-Specific Configuration
Configure CORS based on environment:Middleware Order
Testing CORS
Browser Developer Tools
Check CORS headers in browser DevTools Network tab:- Open DevTools (F12)
- Go to Network tab
- Make a cross-origin request
- Look for
Access-Control-*headers in the response
cURL Testing
Test with cURL:Postman Testing
Postman does not enforce CORS restrictions. Use a browser or curl for accurate CORS testing.
Common Issues
CORS Error: No 'Access-Control-Allow-Origin' Header
CORS Error: No 'Access-Control-Allow-Origin' Header
Cause: CORS is not configured or origin is not allowedSolution:
- Verify CORS middleware is added
- Check the requesting origin is in allowed origins
- Ensure middleware order is correct
CORS Error with Credentials
CORS Error with Credentials
Cause: Using
AllowAnyOrigin() with AllowCredentials()Solution:- Replace
AllowAnyOrigin()withWithOrigins("specific-origin") - Or remove
AllowCredentials()if credentials aren’t needed
Preflight Request Fails
Preflight Request Fails
Cause: OPTIONS requests are not being handledSolution:
- Ensure CORS middleware is before authentication
- Check firewall/load balancer allows OPTIONS
- Verify no [DisableCors] attribute on controller
CORS Works in Development, Not Production
CORS Works in Development, Not Production
Cause: Different CORS configuration per environmentSolution:
- Check production configuration settings
- Verify production origins are correctly specified
- Check for HTTPS requirement in production
Security Considerations
Validate Origins
Validate Origins
- Always validate origins in production
- Use a whitelist of allowed origins
- Consider using pattern matching for subdomains
- Store allowed origins in configuration, not code
Be Careful with Credentials
Be Careful with Credentials
- Only enable credentials when necessary
- Must specify exact origins when using credentials
- Consider the security implications of cross-origin cookies
Limit Methods and Headers
Limit Methods and Headers
- Only allow necessary HTTP methods
- Restrict headers to those actually needed
- Use specific policies for different API sections
Monitor and Log
Monitor and Log
- Log CORS violations
- Monitor for unusual cross-origin requests
- Review and update allowed origins regularly
Installation
Dependencies
Intent.AspNetCore(>= 4.0.2)Intent.Common.CSharpIntent.OutputManager.RoslynWeaver
Next Steps
Controllers
Create endpoints that support CORS
Security
Add authentication with CORS
ASP.NET Core
Learn about the core infrastructure
