1、移除X-AspNet-Version
在 web.config 中加入该行代码
<system.web>
<httpRuntime enableVersionHeader="false"/>
2、移除X-Powered-By
<system.webServer>
<httpProtocol>
<customHeaders>
<remove name="X-Powered-By" />
3、移除Server和X-AspNetMvc-Version
protected void Application_PreSendRequestHeaders(object sender, EventArgs e)
{
HttpApplication app = sender as HttpApplication;
if (app != null && app.Context != null)
{
//移除Server
app.Context.Response.Headers.Remove("Server");
//移除X-AspNetMvc-Version,和上面效果一样
app.Context.Response.Headers.Remove("X-AspNetMvc-Version");
}
}
通过以上设置,我们正常访问接口会发现Server都返回头部信息都不见了,但是我们访问静态资源文件再次发现:

那怎么办呢?
1、设置所有请求经过管道:
<modules runAllManagedModulesForAllRequests="true"/>
但是这样有损网站访问性能
2、UrlScan:微软提供的(此方法代码有侵入,个人不喜欢直接不用)
3、StripHeaders:一个C++开发的开源模块,使用 WIN32 API 在 IIS 核心执行,能涵盖静态内容,核心模块的 Overhead 低,加上原生程序执行效能远比 .NET 程序快,不用担心性能问题。
StripHeaders github:https://github.com/Dionach/StripHeaders/releases