c# - 在 ASP.NET MVC 中使用 web.config 保护内容安全策略 header 列表

标签 c# asp.net-mvc iis-7 content-security-policy

e.g. Image file Webconfig file which i manage in my project

e.g. Image file of Issue facing for 404Javascript.js
我在现有的 ASP.NET MVC 项目中面临以下内容安全策略问题。

我们使用了 https://sitecheck.sucuri.net/检查安全扫描

Issue - Security Headers

Missing security header for XSS Protection. Affected pages:

Missing security header to prevent Content Type sniffing. Affected pages:

Missing Strict-Transport-Security security header. Affected pages:

Default server banners displayed. Your site is displaying your web server default banners. Affected pages:
https://www.example.com/404javascript.js
https://www.example.com/404testpage4525d2fdc



我们正在尝试使用具有以下配置的 web 配置文件来解决该问题:
<customHeaders>
  <remove name="X-Powered-By" />
  <add name="X-Frame-Options" value="DENY" />
  <add name="X-Xss-Protection" value="1; mode=block" />
  <add name="X-Content-Type-Options" value="nosniff" />
  <add name="Referrer-Policy" value="no-referrer" />
  <add name="X-Permitted-Cross-Domain-Policies" value="none" />
  <add name="Strict-Transport-Security" value="max-age=31536000; includeSubDomains" />
  <add name="Feature-Policy" value="accelerometer 'none'; camera 'none'; geolocation 'none'; gyroscope 'none'; magnetometer 'none'; microphone 'none'; payment 'none'; usb 'none'" />
</customHeaders>

<httpRuntime targetFramework="4.6.1" maxRequestLength="1048576"  
             requestValidationMode="4.0" executionTimeout="110" 
             enableVersionHeader="false" />

但是在更改配置后我们仍然面临同样的问题,请查看我们的过程中我们出错的地方或任何替代解决方案。

最佳答案

确保在 system.webserver 部分而不是在 system.web 部分中添加自定义标题标记。:

<system.webServer>
  <httpProtocol>
    <customHeaders>

      <add name="X-Frame-Options" value="SAMEORIGIN" />

      <add name="Strict-Transport-Security" value="max-age=31536000; includeSubDomains"/>

      <add name="X-XSS-Protection" value="1; mode=block" />

      <add name="X-Content-Type-Options" value="nosniff" />

      <add name="Content-Security-Policy" value="default-src 'self'; font-src *;img-src * data:; script-src *; style-src *;" />

      <add name="Referrer-Policy" value="strict-origin" />
    </customHeaders>
  </httpProtocol>
</system.webServer>

执行 chnages 后重新启动 iis 服务器。

关于c# - 在 ASP.NET MVC 中使用 web.config 保护内容安全策略 header 列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57145278/

相关文章:

c# - 如何保留数据的旧值和新值?

c# - .Net 中的大容量、高速文本框

c# - 如何修复,TextBox 接受 int

asp.net-mvc - 为什么我的HTML帮助器没有智能感知功能?

asp.net-mvc - MVC4 输入字段占位符

c# - 如何获取我的 Web 应用程序的基本 URL?

asp.net - 使用 WindowsAuthenticationModule 时在哪里添加 WWW-Authenticate header

asp.net - 在 Windows Server 2008 IIS7 上写入 ASP.NET 中的事件日志

c# - 在 C# 中编写递归函数

c# - 使用 Asp.Net MVC C# 使用谷歌地图 API 计算 2 个邮政编码之间的距离