Roadmap › ASP.NET Core Web API & REST › Module: Security
Lessons in this course (15)

10. CORS, HTTPS, rate limiting and API keys

Intermediate★ Must knowLesson 10 of 15
csharp
builder.Services.AddCors(o => o.AddPolicy("spa", p => p.WithOrigins("https://app.example.com")
    .AllowAnyHeader().AllowAnyMethod().AllowCredentials()));
app.UseCors("spa");

builder.Services.AddRateLimiter(o =>
{
    o.RejectionStatusCode = StatusCodes.Status429TooManyRequests;
    o.AddFixedWindowLimiter("per-client", l => { l.PermitLimit = 100; l.Window = TimeSpan.FromMinutes(1); });
    // also: sliding window, token bucket, concurrency limiters; partition by user/IP with PartitionedRateLimiter
});
app.UseRateLimiter();
app.MapGet("/api/search", Search).RequireRateLimiting("per-client");
  • CORS is a browser mechanism: the server tells browsers which other origins may call it (preflight OPTIONS for non-simple requests). It is not authentication — non-browser clients ignore it. Never AllowAnyOrigin with credentials (not allowed anyway).
  • Other protections: HTTPS only, input validation, request size limits, API keys for partner identification (not as the only auth), secrets in Key Vault, security headers.