How can I modify HTTP headers in ASP.NET (Web Forms) that are defined in web.config

孤者浪人 提交于 2021-02-07 14:22:54

问题


I have some custom HTTP response headers defined in an ASP.NET (Web Forms) website's web.config.

I'm trying to write a managed HTTP module that can modify these web.config defined headers before they're sent back to the client. Unfortunately, whatever event I use (PreSendRequestHeaders, EndRequest) , the headers defined in web.config do not exist in the Response.Headers collection.

I'm aware that PreSendRequestHeaders is not recommended so I also tried using Response.AddOnSendingHeaders but this doesn't give me access to the headers either.

Specifically what I'm trying to do is address inline script for our Content Security Policy (CSP). I'd like to automatically add a nonce attribute to all inline script blocks (I have a working POC for this) and then modify the HTTP CSP header (defined in web.config) to add the 'nonce-' items into the script-src section.

I don't want to add the CSP header in code because it changes often enough that recompile and publish of the website would be a pain.

Do I need to resort to a native IIS module to achieve this or is there some way to do it using managed code (preferred)?

来源:https://stackoverflow.com/questions/49128302/how-can-i-modify-http-headers-in-asp-net-web-forms-that-are-defined-in-web-con

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!