Asp.net 如何在IIS中为内容安全策略使用nonce?
我在ASP.NET4.5.1中开发了MVC应用程序。应用程序在几个页面中使用javascripts。一些Javascript被引用为Asp.net 如何在IIS中为内容安全策略使用nonce?,asp.net,asp.net-mvc,security,content-security-policy,Asp.net,Asp.net Mvc,Security,Content Security Policy,我在ASP.NET4.5.1中开发了MVC应用程序。应用程序在几个页面中使用javascripts。一些Javascript被引用为 @Scripts.Render("~/Scripts/bootstrap") @Scripts.Render("~/Scripts/js") 还有一些内联脚本,比如 <script type="javascript"> // javascript code </script> 因此,每个响应都将包含此标题 问题 1.是否只建议内
@Scripts.Render("~/Scripts/bootstrap")
@Scripts.Render("~/Scripts/js")
还有一些内联脚本,比如
<script type="javascript">
// javascript code
</script>
因此,每个响应都将包含此标题
问题1.是否只建议内联脚本使用
nonce
属性?2.如何将随机生成的
nonce
添加到script
标记中,重要的是,IIS如何知道随机生成的nonce值,以便它可以包含在响应头中?(我假设每个响应都有唯一的nonce值)
nonce
属性仅用于内联脚本。如果您想从其他来源获取安全的源代码,可以使用hash
CreateNonce()
的随机nonce字符串。字符串长度不是很重要,但需要确保它是唯一的值。所以,如果你用字母数字字符定义一个10-20长度的字符串,就可以了。您将在视图上调用此方法,其中是内联脚本。例如:
object-src 'none';
script-src 'nonce-{random}' 'unsafe-inline' 'unsafe-eval' 'strict-dynamic' https: http:;
base-uri 'self';
report-uri https://csp.withgoogle.com/csp/<unique_id>/<application_version>
Views.cshtml
<script <%CreateNonce()%> >
Helpers.cs
Public CreateNonce(){
generateRandomAlphaNumericChars
CSPGenerator('apply-to-csp-rules:generateRandomAlphaNumericChars')
and response.write generateRandomAlphaNumericChars
}
Public CSPGenerator(str nonce_){
GetHttpResponse[csp],
if null, add new response header,
response.header.append="script-src "+ nonce_
}