移除IIS响应中不需要的信息

作者:佚名 来源:网络 点击数: 发布时间:2019年12月05日

1.移除:X-AspNet-Version 
在 web.config 中加入该行代码。


  
  enableVersionHeader="false"/>

2.移除:X-AspNetMvc-Version 
在 Global.asax.cs 文件中加入

protectedvoidApplication_Start(){MvcHandler.DisableMvcResponseHeader=true;}

3.移除或修改:Server
在工程中加入一个module 类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace RC.Website.App_Code{publicclassCustomHeaderModule:IHttpModule{publicvoidInit(HttpApplication context){
            context.PreSendRequestHeaders%2B=OnPreSendRequestHeaders;}publicvoidDispose(){}voidOnPreSendRequestHeaders(object sender,EventArgs e){try{if(HttpContext.Current!=null){HttpContext.Current.Response.Headers.Remove("Server");HttpContext.Current.Response.Headers.Remove("X-AspNet-Version");}}catch(Exception ex){}// 你可以在此设置//HttpContext.Current.Response.Headers.Set("Server", "CERN httpd");}}}

此外还需要在web config 做进一步设置


  
  
  name="CustomHeaderModule"type="RC.Website.App_Code.CustomHeaderModule"/>
    
    
    
    name="CustomHeaderModule"type="RC.Website.App_Code.CustomHeaderModule"/>

4.移除或更改:X-Powered-By 
打开 IIS 的管理控制台界面(IIS7 Managerment Console)-> HTTP Response Headers