移除IIS 相关头部

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

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容