c# - ASP .NET Core 响应 header 中没有 HSTS header

标签 c# asp.net-core asp.net-web-api asp.net-core-2.2

在我的 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/

相关文章:

c# - 如何使用 C# 远程对 Stack Overflow 帖子投反对票?

c# - 如何在 Ubuntu 上运行已经开发的 ASP.NET Core 应用程序?

asp.net - 我的 ASP.Net 代码可以从 sendgrid 获得电子邮件已发送的确认吗?

C#:List<T> 和 Collection<T> 之间的区别(CA1002,不要公开通用列表)

c# - 将 StaticResource 的值传递给组合字符串中的 ConverterParameter

C# .net 核心 web api post 参数始终为 null

c# - 请求被中止 : Could not create SSL/TLS secure channel. |系统.Net.WebException

c# - dotnet 核心 webapi json-api 兼容查询字符串路由

c# - TransactionScopeOption.RequiresNew 的 TransactionScope 超时异常

c# - ASP.NET Core 3.1 中的自定义错误处理中间件?