如何在基于 Windows 的容器内测试应用服务上的安全标头?

问题描述 投票:0回答:1

我已经在我的网络应用程序上设置了 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 应用服务设置其他安全标头:

  • X-XSS-保护
  • X 框架选项
  • X-内容类型-选项
  • HTTP 严格传输安全 (HSTS)
  • 推荐人政策
  • 功能策略
  • 内容安全政策
windows azure header azure-web-app-service hsts
1个回答
0
投票

您可以从

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>

您还可以参考SOThread博客,其中解释了如何从

web.config
文件添加安全标头。

输出: enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.