从WCF中的HTTP响应中删除服务器

我有一个在IIS 7.5上运行的Internet暴露的WCF服务,我需要保护它。 我想删除HTTP响应中的“服务器”标头。

我已经实现了一个IDispatchMessageInspector,代码如下。

public void BeforeSendReply(ref Message reply, object correlationState) { var context = WebOperationContext.Current; if (context != null) { context.OutgoingResponse.Headers.Remove( HttpResponseHeader.Server); } } 

但是,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"); } } }           

编辑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"); } } 

这可以使用IDispatchMessageInspector

 public class SecureBehaviour : IDispatchMessageInspector { public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext) { return null; } public void BeforeSendReply(ref Message reply, object correlationState) { var httpCtx = HttpContext.Current; if (httpCtx != null) { httpCtx.Response.Headers.Remove( HttpResponseHeader.Server.ToString()); } } } 

由于您在IIS中托管服务并且已经启动了HttpModule,因此请尝试设置ASP.NET兼容模式,以便可以访问HttpContext.Current。 您需要进行以下更改:

修改web.config并将以下内容添加到System.ServiceModel

  ...   

使用以下属性装饰您的服务类:

 [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)] 

给HttpModule另一个镜头,你应该有更好的运气。

您是否尝试过编辑web.config并使用system.webServer下的customHeaders标记。

             

这导致我的C#ASP.NET应用程序只有以下响应头:

 HTTP/1.1 200 OK Cache-Control: max-age=3600, public Content-Length: 20992 Content-Type: text/html; charset=utf-8 Content-Encoding: gzip Last-Modified: Tue, 15 May 2012 18:01:11 GMT ETag: "HHktEL5IWA6rspl4Bg2ZxNmnV3gTUCLt2cTldSsl05A=" Vary: Accept-Encoding Date: Tue, 17 Jul 2012 21:38:38 GMT 

虽然我承认我没有尝试使用“服务器”标题,但这种方法似乎运行良好。 我没有尝试使用“Server”标头的原因是我的IHttpModule中的以下代码工作得很好。

  void PreSendRequestHeaders(object sender, EventArgs e) { HttpApplication application = (HttpApplication)sender; if(HttpRuntime.UsingIntegratedPipeline) { application.Response.Headers.Remove("Server"); application.Response.Headers.Remove("Expires"); application.Response.Headers.Remove("Cache-Control"); application.Response.AddHeader("Cache-Control", "max-age=3600, public"); } } 

工作正在进行中。

此博客条目更多地讨论了不同的管道以及ASP.NET模块不能正常工作的原因。

更多要遵循。

对于我自己托管的WCF服务,M Afifi的答案不起作用。 我必须设置一个空头:

 httpCtx.OutgoingResponse.Headers.Add(HttpResponseHeader.Server.ToString(), string.Empty); 

这将从响应中删除标头:

 access-control-allow-headers →Content-Type, Authorization, Accept access-control-allow-methods →GET, POST access-control-allow-origin →* content-type →application/json; charset=utf-8 date →Mon, 03 Jul 2017 07:22:17 GMT status →200 

你有权访问注册表吗? 如果是这样你可以尝试

 HKLM\SYSTEM\CurrentControlSet\Services\HTTP\Parameters\DisableServerHeader