ASP .NET Core 没有 HSTS header 响应 headers

ASP .NET Core no HSTS header in response headers

在我的 appsettings.json 中,我添加了这行代码:

"Hsts": {
    "HstsEnable": true
 }

launchSettings.json 我添加了 https://localhost:5000:

"applicationUrl": "http://localhost:5001;https://localhost:5000"

然后,在 Program.cs 中我使用了这个网址:

 return WebHost.CreateDefaultBuilder(args)
            .UseKestrel(x => x.AddServerHeader = false)
            .UseUrls("http://localhost:5001", "https://localhost:5000")
            .UseStartup<Startup>()

在启动 class 中,在 Configure 方法中,我从 appSettings.json:

中获取 Hsts
if (Configuration.GetSection("Hsts").GetValue<bool>("HstsEnable"))
{
    app.UseHsts();
}

app.UseHttpsRedirection();

完成所有这些步骤后,我无法得到 Strict-Transport-Security。我从响应 header 中得到的是:

 cache-control: no-store,no-cache 
 content-type: application/json; charset=utf-8 
 pragma: no-cache 

Hsts 减少了 header 的响应。如果没有所有这些代码行(在我的应用程序中设置 hsts),我会得到这个响应 headers:

access-control-allow-credentials: true 
access-control-allow-origin: * 
access-control-expose-headers: Content-Disposition 
cache-control: no-store,no-cache 
content-type: application/json; charset=utf-8 
date: Fri, 11 Oct 2019 09:21:30 GMT 
pragma: no-cache 
transfer-encoding: chunked 
vary: Origin 
x-frame-options: DENY 
x-stackifyid: id

所以这个 Hsts 有问题。

如何添加 HSTS header 作为响应 header,我在上面提到过?我需要将 header 硬编码到我的 Configure 方法吗?

context.Response.Headers.Add("Strict-Transport-Security", "max-age=31536000");

来自官方文档HTTP Strict Transport Security Protocol (HSTS)

UseHsts 排除以下环回主机:

  • localhost : IPv4环回地址。

  • 127.0.0.1 : IPv4环回地址。

  • [::1] : IPv6环回地址。

您可以尝试发布网络应用并检查 header Strict-Transport-Security.