我有一個互聯網公開的WCF服務在IIS 7.5上運行,我需要保護它。 我想刪除HTTP響應中的「服務器」標題。從WCF中的HTTP響應中刪除服務器
我已經使用代碼實現了一個IDispatchMessageInspector,如下所示。
public void BeforeSendReply(ref Message reply, object correlationState)
{
var context = WebOperationContext.Current;
if (context != null)
{
context.OutgoingResponse.Headers.Remove(
HttpResponseHeader.Server);
}
}
但是,服務器頭仍在響應中。 在調試中,我可以看到OutgoingResponse.Headers
不包括HttpResonseHead.Server
,如果我編寫自己的值,它顯然正在被IIS管道中更深層次的東西所過度考慮。
編輯1
試過以下,沒有好或者
public class SecureServerHeaderModule : IHttpModule
{
#region Implementation of IHttpModule
public void Init(HttpApplication context)
{
context.PreSendRequestHeaders += OnPreSendRequestHeaders;
}
public void Dispose() { }
#endregion
private static void OnPreSendRequestHeaders(object sender, EventArgs e)
{
var context = HttpContext.Current;
if (context != null)
{
context.Response.Headers.Remove("Server");
}
}
}
<system.web>
<httpModules>
<add "snip" />
</httpModlules>
</system.web>
<system.webServer>
<modules>
<add "snip" />
</modlules>
</system.webServer>
編輯2
也沒有工作。
public void BeforeSendReply(ref Message reply, object correlationState)
{
var context = OperationContext.Current;
if (context != null)
{
context.OutgoingMessageProperties.Remove(
HttpResponseHeader.Server.ToString());
context.OutgoingMessageProperties.Add(
HttpResponseHeader.CacheControl.ToString(), "no-store");
}
}
看來@Trey梳子與工作答案只要你,但也許你錯過了一個服務定義或兩個? – 2012-07-16 17:34:16