Securing your ASP.NET Core applications is non-negotiable in today’s cyber-threat landscape. By enforcing HTTPS, HSTS, and TLS, you ensure encrypted communication between clients and servers, protecting sensitive data from interception and attacks.

This guide provides a step-by-step, end-to-end implementation from development to production.

Prerequisites

Optional: Docker/Kubernetes for production deployment.

Step 1: Create an ASP.NET Core Web API Project

  1. Open Visual Studio → Create a new project → Select ASP.NET Core Web API.

  2. Choose .NET 7 , enable HTTPS , and leave Authentication as “None” for now.

  3. Click Create.

You now have a basic Web API project with HTTPS support enabled by default (self-signed certificate for development).

Step 2: Enforce HTTPS

Program.cs

C#
  
    var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

// Force all HTTP requests to HTTPS
app.UseHttpsRedirection();

app.MapControllers();

app.Run();

launchSettings.json

JSON
  
    "profiles": {
  "MyApi": {
    "commandName": "Project",
    "dotnetRunMessages": true,
    "applicationUrl": "https://localhost:5001;http://localhost:5000"
  }
}

With this, your app automatically redirects HTTP requests to HTTPS during development.

Step 3: Enable HSTS (HTTP Strict Transport Security)

HSTS tells browsers to always connect via HTTPS , preventing downgrade attacks.

Add Middleware

C#
  
    if (!app.Environment.IsDevelopment())
{
    // Enable HSTS in production only
    app.UseHsts();
}

app.UseHttpsRedirection();

Optional HSTS Options

C#
  
    app.UseHsts(options =>
{
    options.MaxAge = TimeSpan.FromDays(365); // enforce for 1 year
    options.IncludeSubDomains = true;
    options.Preload = true;
});

Important: Do not enable HSTS during development; browsers may cache HSTS headers and block HTTP requests.

Step 4: Configure TLS in Kestrel

ASP.NET Core uses Kestrel as the web server. Configure TLS to enforce secure protocols.

appsettings.json

JSON
  
    "Kestrel": {
  "Endpoints": {
    "Https": {
      "Url": "https://localhost:5001",
      "Certificate": {
        "Path": "certs/devcert.pfx",
        "Password": "StrongPassword123!"
      }
    }
  }
}

Program.cs

C#
  
    builder.WebHost.ConfigureKestrel(options =>
{
    options.ConfigureHttpsDefaults(httpsOptions =>
    {
        // Enforce TLS 1.2 and TLS 1.3
        httpsOptions.SslProtocols = System.Security.Authentication.SslProtocols.Tls12 |
                                   System.Security.Authentication.SslProtocols.Tls13;
    });
});

Step 5: Add Secure Cookies (Optional)

If your API or MVC app uses cookies:

C#
  
    builder.Services.AddSession(options =>
{
    options.Cookie.HttpOnly = true;
    options.Cookie.SecurePolicy = CookieSecurePolicy.Always;
    options.Cookie.SameSite = SameSiteMode.Strict;
});

Step 6: Production Certificates

Option 1. Use a Trusted CA

Option 2. Let’s Encrypt (Free & Auto-Renewing)

Step 7: Enforce Security Best Practices

  1. Redirect all HTTP requests to HTTPS.

  2. Enable HSTS with preload & includeSubDomains.

  3. Enforce TLS 1.2+.

  4. Use secure cookies with HttpOnly and SameSite .

  5. Rotate certificates regularly.

  6. Test with SSL Labs for vulnerabilities.

Step 8: Testing

Step 9: CI/CD & Deployment Considerations

By following these steps, you ensure your ASP.NET Core application is:

Security starts with encryption, and with HTTPS + HSTS + TLS, you protect your users’ data from interception and attacks.

Final Thoughts

While HTTPS, HSTS, and TLS secure your data in transit, they are just one part of a larger security strategy. Combine them with:

I can also prepare a ready-to-download Visual Studio demo project with: