Manage cross-origin requests securely using a fluent configuration builder.
CORS is a security mechanism that allows a server to indicate which origins (domains) are permitted to access its resources. By default, browsers block cross-origin requests for security reasons.
To allow any origin during development:
App.UseCors(procedure(Builder: TCorsBuilder)
begin
Builder
.AllowAnyOrigin
.AllowAnyMethod
.AllowAnyHeader;
end);For production, always specify your domains:
App.UseCors(procedure(Builder: TCorsBuilder)
begin
Builder
.WithOrigins(['https://myapp.com', 'https://www.myapp.com'])
.WithMethods(['GET', 'POST', 'PUT', 'DELETE'])
.WithHeaders(['Content-Type', 'Authorization'])
.AllowCredentials
.WithMaxAge(3600); // Cache preflight response for 1 hour
end);| Method | Description |
|---|---|
WithOrigins(['...']) |
Define permitted domains. |
AllowAnyOrigin |
Allow any origin (*). |
WithMethods(['...']) |
Define permitted HTTP verbs. |
AllowAnyMethod |
Allow any HTTP verb. |
WithHeaders(['...']) |
Define permitted request headers. |
AllowAnyHeader |
Allow any request header. |
WithExposedHeaders(['...']) |
Headers the client is allowed to see. |
AllowCredentials |
Enable cookie/auth header sharing. |
WithMaxAge(seconds) |
Sets how long preflight results can be cached. |
AllowAnyOriginvsAllowCredentials: Most browsers will reject a response if it allows any origin while also allowing credentials. You must specify explicit origins if you need credentials.- Order Matters: CORS middleware should be one of the first components in the pipeline to properly handle
OPTIONSpreflight requests.