Orchardcore cors

From CrestApps/CrestApps.AgentSkills · View interactive version

Orchardcore cors is an AI agent skill from the GitHub repository CrestApps/CrestApps.AgentSkills. Skill for administering tenant-specific cross-origin resource sharing in Orchard Core. Covers CorsSettings policies, CorsPolicySetting options, the settings recipe step, CorsService persistence, CorsOptionsConfiguration, admin configuration, deployment, and secure origin restrictions. Use this skill

How to use this skill

Skill file

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

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

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"
Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.