是否可以编辑由IIS / web.config设置的自定义Content-Security-Policy标头?



我想知道是否可以修改在web.config<customHeaders>下设置的Content-Security-Policy标头。

如果可能的话,我想注入一个随机数值。我目前正在这样做,但我需要从web.config中完全删除标头并通过Application_BeginRequest()添加它

我已经在global.asax上戳了一下来抓住标题。在管线的这一点上,它似乎不存在。我只能假设它是稍后注入的?

<customHeaders>
<add name="Content-Security-Policy" value="myCsp nonce-{injectMe}" />
</customHeaders>
// would like to do something akin to this:
protected void Application_BeginRequest()
{
var nonce = 'myNonce';
Response.Headers["Content-Security.Policy"] =
Response.Headers["Content-Security.Policy"].Replace("{injectMe}", nonce);
}

目标是将CSP保留在web.config中,而不必在更改它时重新构建。

这可能吗?

我相信你要找的是Context.Response.Headers["Content-Security.Policy"]。Replace("{injectMe}", nonce(;

如果我是对的,这应该抓住正在启动的请求的当前上下文。

最新更新