为什么不删除Server和X-Powered-By标头?

Jam*_*mes 5 asp.net iis web-config global-asax http-headers

我的ASP.NET 4.5应用程序正在部署到共享主机,因此我无法访问IIS设置.要删除X-Powered-By标题,我指定web.config:

<system.webServer>
  <httpProtocol>
    <customHeaders>
      <remove name="X-Powered-By" />
    </customHeaders>
  </httpProtocol>
</system.webServer>
Run Code Online (Sandbox Code Playgroud)

要删除Server标题,我指定Global.asax:

protected void Application_PreSendRequestHeaders(object sender, EventArgs e) {
  HttpContext.Current.Response.Headers.Remove("Server");
}
Run Code Online (Sandbox Code Playgroud)

但是,响应仍包含两个标头:

Cache-Control:private
Content-Encoding:deflate
Content-Length:672
Content-Type:text/html; charset=utf-8
Date:Sun, 06 Jan 2013 00:41:20 GMT
Server:Microsoft-IIS/7.5
X-Powered-By:ARR/2.5
X-Powered-By:ASP.NET
Run Code Online (Sandbox Code Playgroud)

我该如何删除它们?

Owe*_*ker 5

我不确定为什么X-Powered-By不删除您的文件,但是今年早些时候的Windows Update补丁程序使它Application_PreSendRequestHeaders不再Server:为我们删除标题。

我们必须system.webServer使用IIS URL重写模块2在我们的块中(在Web.config中)添加一个部分:

<rewrite>
    <outboundRules>
        <rule name="Remove RESPONSE_Server">
            <match serverVariable="RESPONSE_Server" pattern=".+"/>
            <action type="Rewrite" value=""/>
        </rule>
    </outboundRules>
</rewrite>
Run Code Online (Sandbox Code Playgroud)


Jam*_*mes 0

如果您使用 IIS 7,则在 Global.asax 中将DisableMvcResponseHeader属性设置为 true 应该删除“X-Powered-By”标头

protected void Application_Start()
{
    MvcHandler.DisableMvcResponseHeader = true;
}
Run Code Online (Sandbox Code Playgroud)