我有一个互联网公开的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