2012-07-12 84 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