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

C# 从WCF中的HTTP响应中删除服务器,c#,wcf,iis-7.5,C#,Wcf,Iis 7.5,我有一个在IIS 7.5上运行的internet公开WCF服务,我需要保护它。 我想删除HTTP响应中的“服务器”头 我实现了一个IDispatchMessageInspector,代码如下 public void BeforeSendReply(ref Message reply, object correlationState) { var context = WebOperationContext.Current; if (context != null) {

我有一个在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);
    }
}
但是,服务器头仍在响应中。 在调试时,我可以看到
OutgoingResponse.Headers
不包括
httpresonsead.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>

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

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

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

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

<system.serviceModel>
  ...
  <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
</system.serviceModel>
再给HttpModule一次机会,您的运气会更好。

工作正在进行中

这进一步回答了不同管道的问题,以及为什么ASP.NET模块不能正常工作


更多信息。

您是否尝试过编辑web.config并使用下面的标记

虽然我承认我还没有尝试过使用“服务器”头,但这种方法似乎工作得很好。我没有尝试使用“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");
        }
    }

这可以通过使用
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());
        }
    }
}

对于我的自托管WCF服务,来自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

PS绝对不是HttpModule,当我通过WCF时,HttpContext.Current为null。这会引发异常System.InvalidOperationException:无法激活该服务,因为它不支持ASP.NET兼容性。已为此应用程序启用ASP.NET兼容性。在web.config中关闭ASP.NET兼容模式,或将AspNetCompatibilityRequirements属性添加到RequirementsMode设置为“Allowed”或“Required”的服务类型中。我可以重现上述错误的唯一方法是对未使用[AspNetCompatibilityRequirements…]”属性。是否有对缺少此属性的服务进行的服务调用?似乎@Trey Combs为您提供了一个有效的答案,但可能您遗漏了一两个服务定义?感谢Microsoft的实际回答检查静态文件请求标题。
    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");
        }
    }
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());
        }
    }
}
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