2017-05-04 64 views
1

我創建了兩個asp.net + MVC應用程序,並將一個Azure App Service Web應用程序和其他應用程序部署到創建的應用程序服務Web應用程序中在ASE(應用程序服務環境)中。如何從App Service Web App上的http響應頭中刪除「Microsoft-HTTPAPI/2.0」

在URL中提供特殊字符時,響應標頭由「Microsoft-HTTPAPI/2.0」組成。我已經在應用程序中完成了下面的更改,但問題仍然存在。

<security> 
    <requestFiltering removeServerHeader="true"/> 
</security> 

protected void Application_PreSendRequestHeaders(Object source, EventArgs e) 
{ 
    HttpContext.Current.Response.Headers.Remove("Server"); 
} 

回答

1

刪除默認標題。你可以創建一個http模塊來完成它。以下代碼供您參考。

public class RemoveDefaultHeaderModule : IHttpModule 
{ 
    public void Dispose() 
    { 

    } 

    public void Init(HttpApplication context) 
    { 
     context.PreSendRequestHeaders += Context_PreSendRequestHeaders; 
    } 

    private void Context_PreSendRequestHeaders(object sender, EventArgs e) 
    { 
     //Remove the header you wanted 
     (sender as HttpApplication).Response.Headers.Remove("Server"); 
     (sender as HttpApplication).Response.Headers.Remove("X-AspNet-Version"); 
    } 
} 

您還需要在web.config中註冊此模塊。不要忘記將runAllManagedModulesForAllRequests屬性設置爲true,這將使該模塊適用於靜態資源。

<system.webServer> 
    <modules runAllManagedModulesForAllRequests="true"> 
    <add name="RemoveDefaultHeaderModule" type="TestServerHeader.RemoveDefaultHeaderModule" /> 
    </modules> 
</system.webServer> 
+0

嗨,感謝您的寶貴意見。我已經嘗試了你的建議,但我仍然在響應中看到'Microsoft-HTTPAPI/2.0',當我在URL中提供特殊字符時 –

+0

你是什麼意思的特殊字符?你能提供一個示例URL來測試嗎? – Amor

0

我使用Windows Server 2016 IIS 10.0,我得到了它運行的PowerShell(如管理員)工作,也做了以下內容:

PS > cd IIS:\ 
PS > Set-WebConfigurationProperty -filter "system.webServer/security/requestFiltering" -name "removeServerHeader" -value "True" 

在web.config與此相結合:

<security> 
    <requestFiltering removeServerHeader="true" /> 
</security> 

作爲參考,我衝這個blog post

但是,當我編輯web.config出於任何原因時,我必須重新運行腳本以便再次刪除服務器標頭......或者它在更新web.config後似乎在短時間內存在。 ..

希望這個幫助!