Asp.Net Remove Unwanted Headers

原文:http://blogs.msdn.com/b/varunm/archive/2013/04/23/remove-unwanted-http-response-headers.aspx

原文:http://blog.paulbouwer.com/2013/01/09/asafaweb-excessive-headers-and-windows-azure/

Server

在Global.asax.cs中添加红色部分代码

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
        }
protected void Application_PreSendRequestHeaders(object sender, EventArgs e) { HttpContext.Current.Response.Headers.Remove("Server"); }

如果使用的是Web Api 2.0的话,以上方法不支持,需要修改注册表

@echo off
setlocal
set regpath=HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\HTTP\Parameters
reg add %regpath% /v DisableServerHeader /t REG_DWORD /d 00000001

复制上方代码,保存为批处理文件,然后运行

另外种方法借助IIS URL Rewrite Module,添加如下的重写规则:

复制代码
 
<system.webServer>
<rewrite>
        <allowedServerVariables>
            <add name="REMOTE_ADDR" />
        </allowedServerVariables>            
        <outboundRules>
            <rule name="REMOVE_RESPONSE_SERVER">
                <match serverVariable="RESPONSE_SERVER" pattern=".*" />
                <action type="Rewrite" />
            </rule>
        </outboundRules>
</rewrite>
</system.webServer>
复制代码

重写规则存放在C:\Windows\System32\inetsrv\config\applicationHost.config中。

X-Powered-By

在web.config中添加以下代码

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

X-AspNet-Version

在web.config中添加以下代码

<system.web>
    <httpRuntime enableVersionHeader="false" />
</system.web>

X-AspNetMvc-Version

在Global.asax.cs中添加红色部分代码

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            MvcHandler.DisableMvcResponseHeader = true;
        }

或者在nuget下添加NWebsec包,移除以上Headers,未测试

转载于:https://www.cnblogs.com/varorbc/p/4463406.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值