2012-07-12 81 views
4

我有一个互联网公开的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"); 
    } 
} 
+0

看来@Trey梳子与工作答案只要你,但也许你错过了一个服务定义或两个? – 2012-07-16 17:34:16

回答

3

这个工程使用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()); 
     } 
    } 
} 
+0

感谢Microsoft提供的实际答案 – 2012-11-06 14:07:27

-1

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

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

也不好。 – 2012-07-12 15:53:13

+0

PS肯定不是一个HttpModule,当我通过WCF的时候HttpContext.Current为null。 – 2012-07-12 15:56:48

+0

也没有工作,它已经设置为1. – 2012-07-16 09:00:55

1

既然你正在主持在IIS服务,并已经纺了一个HTTP模块,尝试设置ASP.NET兼容模式,这样你可以得到HttpContext.Current。你需要做以下修改:

修改你的web.config并添加以下到System.ServiceModel

<system.serviceModel> 
    ... 
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/> 
</system.serviceModel> 

与此属性装饰你的服务类:

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)] 

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

+0

抛出一个异常System.InvalidOperationException:该服务无法激活,因为它不支持ASP.NET兼容性。此应用程序启用了ASP.NET兼容性。关闭web.config中的ASP.NET兼容模式,或将AspNetCompatibilityRequirements属性添加到R​​equirementsMode设置为“允许”或“必需”的服务类型。 – 2012-07-16 09:09:09

+1

我可以重现上述错误的唯一方法是对尚未用“[AspNetCompatibilityRequirements ...]”属性修饰的服务进行服务调用。有没有对缺少此属性的服务进行服务调用? – 2012-07-16 11:48:31

0

工作正在进行中。

这个blog entry回答了一些关于不同的管道以及为什么ASP.NET模块不工作。

更多内容请看。

1

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

<?xml version="1.0" encoding="utf-8"?> 
<configuration> 
    <system.webServer> 
    <httpProtocol> 
     <customHeaders> 
     <remove name="Server" /> 
     <remove name="X-Powered-By" /> 
     <remove name="X-AspNet-Version" /> 
     </customHeaders> 
    </httpProtocol> 
    </system.webServer> 
</configuration> 

这导致仅具有下列响应头我的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 

虽然我承认我还没有与“服务器”头试了一下,这种做法似乎工作得很好。我没有使用“服务器”标题尝试它的原因是,我的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"); 
     } 
    } 
+0

检查静态文件请求标头。 – Dementic 2016-01-14 08:06:12

0

对于我的自承载WCF服务的M·阿菲菲答案不起作用。我必须设置一个空的头:

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