net 中去掉http头信息中的一些信息

enableVersonHeader:指定 ASP.NET 是否应输出版本标头。使用该属性来确定当前使用的 ASP.NET 版本。对于生产环境,该属性不是必需的,可以禁用。

配置示例:

1.<httpRuntime enableVersionHeader="false" />

2.<httpProtocol>
        <customHeaders>
          <remove name="X-Powered-By" />
        </customHeaders>
 </httpProtocol>



 移除Server呢, 我们可以写一个自定义HttpModule,看下来代码:

namespace MyWeb
   {
    public class RemoveServerInfoModule: IHttpModule
     {
        #region IHttpModule Members
  
        public void Dispose(){
           //no code nescessary
         }
         
        public void Init(HttpApplication context)
         {
            context.PreSendRequestHeaders += new EventHandler(context_PreSendRequestHeaders);
         }
   
         void context_PreSendRequestHeaders(object sender, EventArgs e)
          {
              // strip the "Server" header from the current Response 
               HttpContext.Current.Response.Headers.Remove("Server");
         }
   
         #endregion
      }
 }


  
上面这段代码会arise exceptioin,我们最好这样实现PreSendRequestHeaders方法:

        void context_PreSendRequestHeaders(object sender, EventArgs e)
         {
            try
             {
                HttpApplication app = sender as HttpApplication;
                 if (null != app && null != app.Request && !app.Request.IsLocal && null != app.Context && null != app.Context.Response)
               {
                   var headers = app.Context.Response.Headers;
                    if (null != headers)
                   {
                         headers.Remove("Server");
                    }
               }
            }
             catch (Exception ex)
             {
                  Log.HandleException(ex);
            }
        }


  
         最后在Web.config中配置这个HttpModule:

    <httpModules>
      <add name="RemoveServerInfoModule" type="MyWeb.RemoveServerInfoModule"/>
    </httpModules>



  For IIS 7:


  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true" >
      <add name="RemoveServerInfoModule" type="MyWeb.RemoveServerInfoModule"/>
    </modules>
  </system.webServer>




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值