cors-configuration

Configure CORS policies for .NET APIs with origin allowlists and preflight caching.

4|1|Updated Mar 15, 2026
One-click install
npx skills add https://github.com/FaysilAlshareef/dotnet-ai-kit --skill cors-configuration-faysilalshareef
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: cors-configuration
Source: https://github.com/FaysilAlshareef/dotnet-ai-kit/tree/main/skills/security/cors-configuration
Command: npx skills add https://github.com/FaysilAlshareef/dotnet-ai-kit --skill cors-configuration-faysilalshareef

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Browsers block or restrict cross-origin requests when CORS is misconfigured, causing frontend failures, broken authentication flows, and inadvertent exposure of endpoints. This guidance helps teams avoid common CORS mistakes and safely enable only the cross-origin interactions their clients require.

Core Features & Use Cases

  • Least-privilege allowlists for origins, methods, and headers to reduce attack surface.
  • Credential-safe configuration that prevents wildcard origins when cookies or authorization headers are used.
  • Named policies and per-endpoint application so different API groups (public vs authenticated) get appropriate rules.
  • Middleware ordering and preflight caching recommendations, plus environment-specific configuration patterns and an options class for strong typing.
  • Use cases: browser-based SPA on a different origin, third-party front-ends calling public endpoints, development servers on alternate ports, and scenarios requiring subdomain support.

Quick Start

Configure a restrictive CORS policy that allows only https://app.example.com, enables credentials for authenticated SPA requests, and applies the policy to the API endpoints that serve the SPA.

Frequently Asked Questions about cors-configuration

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I configure CORS in a .NET API to allow a specific SPA origin with credentials?

Configuring CORS for a .NET API involves setting an explicit origin allowlist for your SPA, enabling credentials, and applying named policies per endpoint to ensure browser requests are securely authorized without wildcard exposure.

Why does my .NET CORS policy fail when using cookies or authorization headers?

CORS policies fail with cookies or authorization headers because browsers block wildcard origins. You must configure credential-safe CORS by explicitly allowlisting exact origins rather than using wildcards for authenticated requests.

What is the best way to apply different CORS policies to public and authenticated .NET endpoints?

The best way to manage different CORS rules is applying named policies per endpoint. This allows you to assign specific origin allowlists and credential rules selectively to public versus authenticated .NET API routes.

How does preflight caching work for cross-origin requests in .NET middleware?

Preflight caching in .NET reduces browser OPTIONS requests by configuring allowed origins, methods, and headers. Correct CORS middleware ordering ensures preflight responses are served before endpoint execution to optimize cross-origin traffic.

Do I need a separate CORS policy for development servers running on alternate ports?

Development servers on alternate ports require environment-specific CORS configuration. Establish distinct policies for development origins to safely enable local cross-origin testing without exposing production endpoints.

When should I restrict allowed methods and headers in a .NET CORS policy?

Restrict allowed methods and headers in .NET CORS policies to enforce least-privilege. Explicit allowlists reduce your API attack surface by preventing unauthorized cross-origin interactions from browser-based clients.