I have an Internet-based WCF service running on IIS 7.5 that I need to protect. I would like to remove the “Server” header in the HTTP response.
I implemented IDispatchMessageInspector with code as follows.
public void BeforeSendReply(ref Message reply, object correlationState) { var context = WebOperationContext.Current; if (context != null) { context.OutgoingResponse.Headers.Remove( HttpResponseHeader.Server); } }
However, the server header is still in the response. When debugging, I see that OutgoingResponse.Headers does not include HttpResonseHead.Server , and if I write my own value, it is explicitly redefined by something further down the line in the IIS pipeline.
Change 1
I tried the following: there was nothing good
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>
Edit 2
Also did not work.
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"); } }
source share