在我的 appsettings.json
中,我添加了这行代码:
"Hsts": {
"HstsEnable": true
}
在 launchSettings.json
我添加了 https://localhost:5000
:
"applicationUrl": "http://localhost:5001;https://localhost:5000"
然后,在 Program.cs
中我使用了这个 urls:
return WebHost.CreateDefaultBuilder(args)
.UseKestrel(x => x.AddServerHeader = false)
.UseUrls("http://localhost:5001", "https://localhost:5000")
.UseStartup<Startup>()
在启动类中,在 Configure
方法中,我从 appSettings.json
获取 Hsts
值:
if (Configuration.GetSection("Hsts").GetValue<bool>("HstsEnable"))
{
app.UseHsts();
}
app.UseHttpsRedirection();
完成所有这些步骤后,我无法获得 Strict-Transport-Security
。我从响应头中得到的是:
cache-control: no-store,no-cache
content-type: application/json; charset=utf-8
pragma: no-cache
Hsts
从响应中剪切 header 。如果没有所有这些代码行(在我的应用程序中设置 hsts
),我会得到这个响应 header :
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
有问题。
如何在上面提到的响应 header 中添加 HSTS
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。
关于c# - ASP .NET Core 响应 header 中没有 HSTS header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58338166/