代码改变世界

隐藏ASP.NET MVC的版本信息,使其不在HTTP Header中显示

2024-03-30 11:48  狼人:-)  阅读(11)  评论(0编辑  收藏  举报

隐藏ASP.NET MVC的版本信息,使其不在HTTP Header中显示。

一、隐藏:X-AspNetMvc-Version

在Global.asax文件的Application_Start方法中添加:

1
MvcHandler.DisableMvcResponseHeader = true;

二、移除 Header 中的 Server

在Global.asax文件中添加:

1
2
3
4
5
6
7
8
9
10
11
protected void Application_PreSendRequestHeaders(object sender, EventArgs e)
       {
           var app = sender as HttpApplication;
           if (app == null || app.Context == null)
           {
               return;
           }
 
           // 移除 Server
           app.Context.Response.Headers.Remove("Server");
       }

三、移除 X-Powered-By

在Web.config文件中添加:

1
2
3
4
5
6
7
8
9
10
11
<system.webServer>
    <!-- 其它内容 -->
    <httpProtocol>
        <customHeaders>
            <!-- 移除 X-Powered-By -->
            <clear />
            <!-- 还可以添加自己的 X-Powered-By 做为标识 -->
            <add name="X-Powered-By" value="bbb.com" />
        </customHeaders>
    </httpProtocol>
</system.webServer>

四、移除X-AspNet-Version

在Web.config文件中<httpRuntime enableVersionHeader="false" />

1
<httpRuntime enableVersionHeader="false" />