asp.net-mvc - 为什么 web.config 中的 Content-Security-Policy 设置不起作用?
问题描述
在一个小型 ASP.NET MVC 测试应用程序中,我将适当的httpProtocol
代码添加到web.config
文件中,如本文所述:
<system.webServer>
<handlers>
<remove name="BlockViewHandler"/>
<add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
</handlers>
<httpProtocol>
<customHeaders>
<add name="Content-Security-Policy" value="default-src 'self'" />
</customHeaders>
</httpProtocol>
</system.webServer>
但是,在应用程序的测试页面上,Vue.js 代码仍然有效,因为它是从 CDN 加载的,因此内容安全策略应该阻止它。
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Home Page2 - Meine ASP.NET-Anwendung</title>
<link href="/Content/bootstrap.css" rel="stylesheet"/>
<link href="/Content/site.css" rel="stylesheet"/>
<script src="/Scripts/modernizr-2.6.2.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.1.4/vue.js"></script>
</head>
<body>
<div class="container mainMenu">
<ul>
<li><a href="/">Start Page</a></li>
<li><a href="/Home/About">About</a></li>
<li><a href="/Home/Contact">Contact</a></li>
<li><a href="/Home/Info">Info</a></li>
</ul>
</div>
<div class="container body-content">
<hr/>
<div id="app">
this is a test: <b>{{message}}</b>
</div>
<script>
var app = new Vue({
el: '#app',
data: {
message: 'Vue.js ready'
}
});
</script>
<hr/>
<footer>
<p>The footer</p>
</footer>
</div>
<script src="/Scripts/jquery-1.10.2.js"></script>
<script src="/Scripts/bootstrap.js"></script>
<script src="/Scripts/respond.js"></script>
</body>
</html>
我必须做什么才能使内容安全策略在我的站点中真正生效?
附录
我可以在我的开发工具中看到 Content-Security-Policy 没有在响应标头中发送:
解决方案
在您的问题中看到下面的配置块,我假设您已将该<httpProtocol><customHeaders> ...
部分添加到web.config
文件Views
夹中的文件而不是web.config
您网站根目录中的文件中。
<system.webServer>
<handlers>
<remove name="BlockViewHandler"/>
<add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
</handlers>
</system.webServer>
推荐阅读
- javascript - Javascript SumOfTwo
- javascript - 将变量连接为函数内的输入名称
- codesandbox - 无法让 CSS 文件在 CodeSandbox 中工作
- php - Windows 上带有 xampp 1.5.5 和带有 apache 的 php 4.5 的本地网站。一些操作后,它不再工作了
- c# - 如何获取注入服务列表到控制器
- git - 提交到已合并的分支并打开新的拉取请求
- c++ - 试图引用已删除的函数 shared_ptr
- amazon-web-services - Elasticsearch 文档不可搜索
- java - 如何为包含 switch 语句的搜索方法返回 ArrayList
- java - SimpleStatement 作为 CQL