Skip to content

Securing end points

To protect .NET API from various attacks, adhere to common web security best practices by implementing the following crucial HTTP headers and middleware. Here's a breakdown of why each part is essential:

  1. app.UseHttpsRedirection();
  • Purpose: Enforces HTTPS communication.
  • Why it's necessary: HTTPS encrypts data transmitted between the client and the server, protecting sensitive information (like passwords, session tokens, and personal data) from eavesdropping and tampering. Without HTTPS, data is sent in plain text, making it vulnerable to man-in-the-middle attacks.


2. app.UseCors();

  • Purpose: Configures Cross-Origin Resource Sharing (CORS).
  • Why it's necessary: CORS controls which origins (domains, protocols, and ports) are allowed to make requests to your API. Without proper CORS configuration, your API could be vulnerable to cross-site scripting (XSS) attacks or other malicious requests from unauthorized origins. CORS needs to be configured correctly, to only allow trusted origins.


3. if (!env.IsDevelopment()) { app.UseHsts(); }

  • Purpose: Implements HTTP Strict Transport Security (HSTS).
  • Why it's necessary: HSTS tells browsers to always use HTTPS when communicating with your API, even if the user types http:// in the address bar. This helps prevent protocol downgrade attacks, where an attacker intercepts an HTTP request and redirects the user to a malicious site. HSTS is generally not used during development, as it can interfere with local testing.


4. app.Use(async (context, next) => { ... }); (Custom Middleware)

  • Purpose: Adds various security-related HTTP response headers.
  • Why each header is necessary:
    • Cache-Control, Pragma, Expires:
      • Purpose: Prevent caching of sensitive data.
      • Why: Caching sensitive data can expose it to unauthorized users, especially on shared computers or public networks. These headers instruct browsers and proxies not to store the response.
    • Referrer-Policy:
      • Purpose: Controls how much referrer information is sent with requests.
      • Why: The referrer header can reveal sensitive information about the user's browsing history. Setting a strict policy (like strict-origin-when-cross-origin) limits the amount of information shared.
    • X-Content-Type-Options:
      • Purpose: Prevents MIME sniffing.
      • Why: MIME sniffing is a technique where browsers try to guess the content type of a response, even if the Content-Type header is incorrect. Setting nosniff prevents this, mitigating potential security vulnerabilities.

csharp
public static void AddSecureHeaders(this WebApplication app, IHostEnvironment env)
{
    app.UseHttpsRedirection();

    app.UseCors();
    
    if (!env.IsDevelopment())
    {
        app.UseHsts(); 
    }
    
    app.Use(async (context, next) =>
    {
        context.Response.Headers["Cache-Control"] = "no-store, no-cache, must-revalidate, proxy-revalidate";
        context.Response.Headers["Pragma"] = "no-cache";
        context.Response.Headers["Expires"] = "0";
        context.Response.Headers["Referrer-Policy"] = "strict-origin-when-cross-origin"; 
        context.Response.Headers["X-Content-Type-Options"] = "nosniff";
        await next();
    });
    
}

Released under the MIT License.