55

I don't quite get the difference between UseHsts and UseHttpsRedirection in the configure section of the startup file in .net core. Could anyone explain?

1
  • 8
    One happens on browser side (HSTS), and the other on server side. Commented Sep 28, 2018 at 17:35

4 Answers 4

49

According to the documentation you should use both together:

We recommend all production ASP.NET Core web apps call:

  • The HTTPS Redirection Middleware (UseHttpsRedirection) to redirect all HTTP requests to HTTPS.
  • UseHsts, HTTP Strict Transport Security Protocol (HSTS).

ASP.NET Core Enforce HTTPS

The .UseHttpsRedirection() will issue HTTP response codes redirecting from http to https. The .UseHsts() will add the HSTS response header which the client is supposed to obey.

Sign up to request clarification or add additional context in comments.

2 Comments

Both UseHttpsRedirection and UseHsts do nothing to block incoming http requests? So basically they are just instructions to the browser?
They do not block incoming requests. UseHttpsRedirection should issue a redirect from http to https. UseHsts is a header to remind the browser that when they come back to this site, skip the initial http request, and go directly to https. If you want to block http completely, you'd need to do that on your host (iis, apache, etc).
11

UseHsts adds the Strict-Transport-Security header to the response, which informs the browser that the application must only be accessed with HTTPS. After this declaration, compliant browsers should automatically convert any http request of the application into an HTTPS request.

UseHttpsRedirection causes an automatic redirection to HTTPS URL when an HTTP URL is received, in a way that forces a secure connection.

Once the first HTTPS secure connection is established, the strict-security header prevents future redirections that might be used to perform man-in-the-middle attacks.

Comments

6

I don't think it is a trivial concept. None of the answers already written does satisfy me. Here is my deductions after reading some articles.


HSTS is abbreviation of Strict-Transport-Security header in HTTP protocol. It informs browsers that the site should only be accessed using HTTPS, and that any future attempts to access it using HTTP should automatically be converted to HTTPS. Using HSTS, the browser will call your application using HTTP only the very first time. The subsequent requests against the same domain will be made using the HTTPS protocol, even in the presence of a URL using the HTTP scheme.

The protection only applies after a user has visited the site at least once, relying on the principle of "trust on first use". The way this protection works is that a user entering or selecting a URL to the site that specifies HTTP, will automatically upgrade to HTTPS, without making an HTTP request, which prevents the HTTP man-in-the-middle attack from occurring.

HSTS is not guaranteed, it may not be supported by your client. It also requires at least one successful HTTPS request to establish the HSTS policy. HSTS is a client side performance optimization to avoid an extra request to the server.

UseHttpsRedirection middleware redirects incoming HTTP requests to HTTPS, ensuring that all communication between the client and server is encrypted. It does redirect to https if properly configured. The risk is that the client may have sent sensitive data on that initial request. HSTS will prevent such leaks in the future, but cannot protect the first request.

I like answers on here as well.

Comments

0

The UseHttpsRedirection must be called with care and should not be always recommended. In cases where the app is not exposed directly but instead, is behind a reverse proxy, it is a common configuration to have HTTPS between the client and the reverse proxy and HTTP from reverse proxy down to the actual app.

In such configuration, calling UseHttpsRedirection could cause issues as the .NET core app would incorrectly redirect calls from the reverse proxy server.

The MSDN page linked in other answers contain a clear and correct information about this particular scenario and anyone following the recommendation should also take this additional information into account.

Apps deployed in a reverse proxy configuration allow the proxy to handle connection security (HTTPS). If the proxy also handles HTTPS redirection, there's no need to use HTTPS Redirection Middleware.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.