Enforcing HTTPS in an ASP.NET Core app

By Rick Anderson

This document shows how to:

  • Require HTTPS for all requests.
  • Redirect all HTTP requests to HTTPS.


Do not use RequireHttpsAttribute on Web APIs that receive sensitive information. RequireHttpsAttribute uses HTTP status codes to redirect browsers from HTTP to HTTPS. API clients may not understand or obey redirects from HTTP to HTTPS. Such clients may send information over HTTP. Web APIs should either:

  • Not listen on HTTP.
  • Close the connection with status code 400 (Bad Request) and not serve the request.

Require HTTPS

The RequireHttpsAttribute is used to require HTTPS. [RequireHttpsAttribute] can decorate controllers or methods, or can be applied globally. To apply the attribute globally, add the following code to ConfigureServices in Startup:

// Requires using Microsoft.AspNetCore.Mvc;
public void ConfigureServices(IServiceCollection services)
    services.Configure<MvcOptions>(options =>
        options.Filters.Add(new RequireHttpsAttribute());

The preceding highlighted code requires all requests use HTTPS; therefore, HTTP requests are ignored. The following highlighted code redirects all HTTP requests to HTTPS:

// Requires using Microsoft.AspNetCore.Rewrite;
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)

    var options = new RewriteOptions()


For more information, see URL Rewriting Middleware.

Requiring HTTPS globally (options.Filters.Add(new RequireHttpsAttribute());) is a security best practice. Applying the [RequireHttps] attribute to all controllers/Razor Pages isn't considered as secure as requiring HTTPS globally. You can't guarantee the [RequireHttps] attribute is applied when new controllers and Razor Pages are added.