我已经在我的网络应用程序上设置了 HSTS。它是一个带有 .net 后端的 Angular 应用程序,托管在 Windows 容器中的 Azure 应用服务上。我对 Startup.cs 进行了更改:
services.AddHsts(options =>
{
options.Preload = true;
options.IncludeSubDomains = true;
options.MaxAge = TimeSpan.FromMinutes(5); //.FromDays(60); //.FromSeconds(31536000);
});
services.AddHttpsRedirection(options =>
{
options.RedirectStatusCode = (int)HttpStatusCode.TemporaryRedirect;
options.HttpsPort = 5001;
});
Chrome 内部显示:dynamic_upgrade_mode: FORCE_HTTPS。 我不知道如何在 Windows 容器中为 Azure 应用服务设置其他安全标头:
您可以从
Security Headers
或 Program.cs
文件中添加 web.config
。
在
Program.cs
文件中,添加以下代码
app.Use(async (mycon, sh) =>
{
mycon.Response.Headers.Add("Strict-Transport-Security", "max-age=31536000; includeSubDomains; preload");
mycon.Response.Headers.Add("X-XSS-Protection", "1; mode=block");
mycon.Response.Headers.Add("X-Frame-Options", "SAMEORIGIN");
mycon.Response.Headers.Add("X-Content-Type-Options", "nosniff");
mycon.Response.Headers.Add("Referrer-Policy", "no-referrer");
mycon.Response.Headers.Add("Feature-Policy", "camera 'none'; microphone 'none'");
mycon.Response.Headers.Add("Content-Security-Policy", "default-src 'self'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline';");
await sh.Invoke();
});
或
我已参考此博客在
web.config
文件中添加安全标头。
web.config
文件并添加以下与安全标头相关的设置。<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<httpProtocol>
<customHeaders>
<clear />
<add name="X-Xss-Protection" value="1; mode=block"/>
<add name="X-Frame-Options" value="SAMEORIGIN"/>
<add name="X-Content-Type-Options" value="nosniff"/>
<add name="Strict-Transport-Security" value="max-age=31536000; includeSubDomains; preload"/>
<add name="Referrer-Policy" value="strict-origin-when-cross-origin"/>
<add name="Feature-Policy" value="Policy-directive" />
<add name="Content-Security-Policy" value="upgrade-insecure-requests; base-uri 'self'; frame-ancestors 'self'; form-action 'self'; object-src 'none';"/>
</customHeaders>
</httpProtocol>
</system.webServer>
</configuration>
web.config
文件添加安全标头。
输出: