Orchard Core CORS - Prompt Templates
Configure Tenant CORS Policies
You are an Orchard Core expert. Generate secure configuration and code for the Orchard Core CORS module.
Guidelines
- Enable the
OrchardCore.Corsfeature. The module has no declared feature dependencies. - The module calls
app.UseCors()at Orchard Core's CORS pipeline order and configures ASP.NET CoreCorsOptionsfrom tenant site settings. - Configure policies from Settings → Security → Cross-Origin Resource Sharing. The page requires the security-critical
ManageCorsSettingspermission (Administrator by default). - Policies are tenant site settings (
CorsSettings). There is noappsettings.jsonconfiguration section for CORS; use the admin UI or thesettingsrecipe step. - Each
CorsPolicySettinghas a name, origin, header, method, credentials, default-policy, and exposed-header settings. - The admin rejects a policy that combines Allow credentials with Allow any origin or an
AllowedOriginsentry of*. At runtime,CorsOptionsConfigurationskips a policy that has bothAllowCredentialsandAllowAnyOrigin(for example one imported by a recipe) and logs a warning. - Use exact trusted origins when credentials are allowed. Do not use wildcard origins for authenticated browser requests.
- The first policy becomes the default if no policy is explicitly marked as default.
- Setting changes release the tenant shell, so the CORS middleware reads the new policy configuration.
- Enable
OrchardCore.Deploymentwhen CORS site settings must be exported by a deployment plan. - All recipe JSON must be wrapped in the root
{ "steps": [...] }format. - All C# classes must use the
sealedmodifier, except View Models.
Enabling CORS
{
"steps": [
{
"name": "Feature",
"enable": [
"OrchardCore.Cors"
],
"disable": []
}
]
}
Configuring Policies in the Admin
Create a policy under Settings → Security → Cross-Origin Resource Sharing and configure:
| Setting | Guidance |
|---|---|
| Name | Use a stable descriptive name such as PublicApi. |
| Allowed origins | Use complete origins such as https://app.example.com. |
| Allowed methods | Limit to the verbs required by the browser client. |
| Allowed headers | Allow only client headers the API actually accepts. |
| Allow credentials | Enable only for trusted, authenticated origins. |
| Exposed headers | List response headers browser JavaScript may read. |
| Default policy | Mark one policy when all endpoints should use it. |
The policy editor refuses wildcard-origin policies that also allow credentials and shows a security warning. Origins are a scheme, host, and optional port only, with no path or trailing slash. An empty allowed list permits nothing for that category unless the matching Allow any option is on.
Configuring Policies via Recipe
{
"steps": [
{
"name": "feature",
"enable": [ "OrchardCore.Cors" ]
},
{
"name": "settings",
"CorsSettings": {
"Policies": [
{
"Name": "Frontend",
"AllowAnyOrigin": false,
"AllowedOrigins": [ "https://app.example.com" ],
"AllowAnyHeader": false,
"AllowedHeaders": [ "Content-Type", "Authorization" ],
"AllowAnyMethod": false,
"AllowedMethods": [ "GET", "POST" ],
"AllowCredentials": true,
"IsDefaultPolicy": true,
"ExposedHeaders": [ "X-Pagination-Total" ]
}
]
}
}
]
}
Recipe and deployment imports write the site settings directly and skip the admin validation. Define at most one default policy, never combine credentials with a wildcard origin, and reload an already-running tenant after importing.
CorsSettings Model
The settings are stored on the tenant site as CorsSettings:
using OrchardCore.Cors.Settings;
var settings = new CorsSettings
{
Policies =
[
new CorsPolicySetting
{
Name = "PublicApi",
AllowedOrigins = ["https://app.example.com"],
AllowedMethods = ["GET", "POST"],
AllowedHeaders = ["Content-Type", "Authorization"],
ExposedHeaders = ["X-Request-Id"],
AllowAnyOrigin = false,
AllowAnyMethod = false,
AllowAnyHeader = false,
AllowCredentials = true,
IsDefaultPolicy = true,
},
],
};
Do not write this model directly from an arbitrary controller. Use the admin UI or a service that deliberately updates tenant site settings and releases the shell.
Reading CORS Settings
CorsService is registered as a singleton and exposes GetSettingsAsync.
using OrchardCore.Cors.Services;
using OrchardCore.Cors.Settings;
namespace MyModule.Services;
public sealed class CorsPolicyReporter
{
private readonly CorsService _corsService;
public CorsPolicyReporter(CorsService corsService)
{
_corsService = corsService;
}
public async Task<IEnumerable<CorsPolicySetting>> GetPoliciesAsync()
{
var settings = await _corsService.GetSettingsAsync();
return settings?.Policies ?? [];
}
}
Endpoint-Specific Policies
The module establishes the configured ASP.NET Core policies. Apply a named policy to an endpoint only when it should not use the default policy.
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
namespace MyModule.Controllers;
[ApiController]
[Route("api/catalog")]
public sealed class CatalogController : ControllerBase
{
[HttpGet]
[EnableCors("PublicApi")]
[AllowAnonymous]
public IActionResult Get()
{
return Ok();
}
}
The policy name must exactly match a policy configured in CorsSettings.
Deployment
When OrchardCore.Deployment is enabled, the module registers a site-settings deployment step labelled Cors settings. Add that step to a deployment plan to export tenant CORS policies; the exported recipe uses the same settings step and CorsSettings object shown above.
{
"steps": [
{
"name": "Feature",
"enable": [
"OrchardCore.Cors",
"OrchardCore.Deployment"
],
"disable": []
}
]
}
Security Checklist
- Never combine
AllowAnyOriginandAllowCredentials. - Do not use an origin path, trailing route, or wildcard where an origin is expected.
- Restrict unsafe methods and non-standard headers.
- Treat CORS as a browser policy, not as authentication or authorization.
- Validate requests with Orchard Core permissions and application authorization even when the origin is trusted.
Troubleshooting
| Symptom | Check |
|---|---|
| No CORS headers | Verify the feature is enabled and the endpoint selects a valid named or default policy. |
| Browser preflight fails | Add only the requested method and headers to the selected policy. |
| Policy is missing after save | Correct the invalid wildcard-origin plus credentials combination. |
| Setting change has no effect | Confirm the tenant shell was released after updating settings. |
| Preflight rejected | Compare the Origin header exactly (scheme and port) and allow every header in Access-Control-Request-Headers. |
| API call from the SPA returns 401 | CORS does not authenticate; send a bearer token for the Api scheme (see orchardcore-openid). |
Test a preflight without a browser:
curl -i -X OPTIONS "https://cms.example.com/api/content/abc" \
-H "Origin: https://app.example.com" \
-H "Access-Control-Request-Method: POST" \
-H "Access-Control-Request-Headers: authorization,content-type"