Skip to main content

Lesson 4 of 8

Security Middleware - HTTPS, HSTS & CORS

Production ASP.NET Core APIs must enforce HTTPS, send HTTP Strict Transport Security headers, and configure CORS correctly. Misconfigured CORS is a major security vulnerability - wildcard origins in production allow any website to call your API with user credentials.

20 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

HTTPS, HSTS & Production CORS

HTTPS, HSTS & Production CORS
// ━━ HTTPS Configuration in Program.cs ━━

// 1. Redirect HTTP → HTTPS (301 permanent redirect)
app.UseHttpsRedirection();

// 2. HSTS - tells browsers to ALWAYS use HTTPS for this domain (even on first visit)
if (!app.Environment.IsDevelopment())
{
    app.UseHsts(); // sends: Strict-Transport-Security: max-age=31536000; includeSubDomains
}

// HSTS configuration in appsettings.json:
builder.Services.AddHsts(options =>
{
    options.MaxAge = TimeSpan.FromDays(365);
    options.IncludeSubDomains = true;
    options.Preload = true; // opt in to browser preload lists
});

// ━━ CORS - production configuration ━━
// WRONG - wilcard + credentials is a security hole:
// builder.Services.AddCors(o => o.AddPolicy("All", p => p.AllowAnyOrigin().AllowCredentials()));

// CORRECT - named policies with specific origins:
builder.Services.AddCors(options =>
{
    options.AddPolicy("ProductionPolicy", policy =>
        policy
            .WithOrigins(
                "https://myapp.com",
                "https://www.myapp.com",
                "https://app.myapp.com")
            .AllowAnyHeader()
            .AllowAnyMethod()
            .AllowCredentials()   // cookies, Authorization header
            .SetPreflightMaxAge(TimeSpan.FromHours(1))); // cache preflight response

    options.AddPolicy("DevelopmentPolicy", policy =>
        policy
            .WithOrigins("http://localhost:3000", "http://localhost:5173")
            .AllowAnyHeader()
            .AllowAnyMethod()
            .AllowCredentials());
});

// Apply policy based on environment:
if (app.Environment.IsDevelopment())
    app.UseCors("DevelopmentPolicy");
else
    app.UseCors("ProductionPolicy");

// ━━ Per-controller/action CORS override ━━
[EnableCors("ProductionPolicy")]
[HttpGet("public")]
public IActionResult PublicEndpoint() => Ok("public");

[DisableCors]
[HttpGet("internal")]
public IActionResult InternalEndpoint() => Ok("no cors for internal");

Tip

Tip

Practice Security Middleware HTTPS HSTS CORS in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.

Diagram
Loading diagram…

.NET provides a unified platform for building various application types

Practice Task

Note

Practice Task - (1) Write a working example of Security Middleware HTTPS HSTS CORS from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.

Quick Quiz

Common Mistake

Warning

A common mistake with Security Middleware HTTPS HSTS CORS is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready dotnet code.

Topics in This Module

0/8 done