Strict Transport Security header missed in API response

Kumar, Vipin [ext] 20 Reputation points
2024-08-14T17:15:35.89+00:00

Hi Team,

We are using the .Net core 8.0(long term support version) for our .Net Core services , we enabled the HSTS at service level by using the

// HSTS Security Headers 
services.AddHsts(options =>
{
options.Preload = true;
options.IncludeSubDomains = true;
options.MaxAge = TimeSpan.FromHours(1);
});
 app.UseHsts();

But when we deployed the services on our stage env and try to call the api and verify the response header Strict Transport Security we missed in response.

pls help me to trouble shoot this.

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,526 questions
{count} votes

Accepted answer
  1. Michael Taylor 53,971 Reputation points
    2024-08-14T17:52:46.2833333+00:00

    HSTS doesn't really apply to APIs. HSTS is for browsers and only browsers enforce them when dealing with rendering. API calls can, in theory, include them but they have no effect, by design. Refer to the docs here where it discusses this in more detail.

    APIs should require HTTPS only and HSTS becomes a mostly mute point after that. If you really, really want to include HSTS headers, even though they have no effect, then you should first confirm it is working locally. Then confirm that you have the calls in the right order. Many of the extension methods require that you call them at specific points in the creation pipeline otherwise they don't work. This is one of the troubling issues with the ASP.NET pipeline. The docs I mentioned earlier show where in the pipeline these calls need to happen.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.