![](/img/trans.png)
[英]How to force IIS to send response headers without sending response body and closing connection
[英]Override IIS Response Headers
由於我在同一IIS實例上托管多個網站,因此我在IIS設置中定義了HTTP響應標頭“ X-Frame-Options:DENY”,以確保我的客戶端(及其客戶端)免受點擊劫持。
我目前正在電子商務網站上使用WebForms,並且需要刪除特定URL(我的付款提供商的回調)上的“ X-Frame-Options”響應標頭。
我已經編寫了一個HTTP模塊(請參見下面的代碼),並將其注冊到我的電子商務網站的“ web.config”中,該代碼有效,但是IIS在執行HttpModule后似乎添加了響應頭。
public class XFrameOptionsControl : IHttpModule
{
public void Dispose()
{
}
public void Init(HttpApplication application)
{
application.PreSendRequestHeaders += new EventHandler(PreSendRequestHeaders);
}
private void PreSendRequestHeaders(object sender, EventArgs e)
{
HttpApplication application = sender as HttpApplication;
HttpContext context = application.Context;
string callbackDocumentName = "callback.html";
if (callbackDocumentName != null)
{
if (!context.Request.RawUrl.ToLower().Contains(callbackDocumentName.ToLower()))
{
context.Response.Headers.Remove("X-Frame-Options");
}
}
}
}
通常,從“ web.config”中檢索“ callbackDocumentName”,但是在示例中我確實簡化了它。 我嘗試使用Visual Studio進行調試,並且'context.Response.Headers'包含以下標頭,但不包含'X-Frame-Options':
提前致謝!
如果要更改響應,則需要在生成響應后進行。
目前,您甚至在將請求發送到您的應用程序之前就已經做到了
嘗試這個
public void Init(HttpApplication context)
{
context.PostRequestHandlerExecute += new EventHandler(this.ProcessResponse);
}
private void ProcessResponse(object sender, EventArgs e)
{
HttpContext context = ((HttpApplication)sender).Context;
//do whatever you want to the context
//context.Response.Headers.Remove
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.