zoukankan      html  css  js  c++  java
  • ASP.Net自定义重写Http Server标头

    Net中我们为了安全或其他原因起见 可能需要修改我们的标头报文等

    以下方法我们通过使用HTTP Module来使用编程的方式来去除或修改它

    首先我们自定义一个类CustomServerHeaderModule继承自IHttpModule 并为PreSendRequestHeaders事件创建事件处理程序

    代码如下:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    
    namespace Cloud.ApiWeb.Models
    {
        public class CustomServerHeaderModule : IHttpModule
        {
            public void Init(HttpApplication context)
            {
                context.PreSendRequestHeaders += OnPreSendRequestHeaders;
            }
            public void Dispose()
            {
            }
            void OnPreSendRequestHeaders(object sender, EventArgs e)
            {
                //移除Server标头
                //HttpContext.Current.Response.Headers.Remove("Server");
                //重新设置Server标头
                HttpContext.Current.Response.Headers.Set("Server", "Windows Server 2012");
            }
        }
    }


    接下来在web.config文件中配置

      <system.webServer>
        <modules runAllManagedModulesForAllRequests="true">
          <add name="FirstModule" type="Cloud.ApiWeb.Models.CustomServerHeaderModule,Cloud.ApiWeb" />
        </modules>
      </system.webServer>

    Type有两部分组成第一部是命名空间及类名,也就是类型名;后面是程序集名。

    如果该类建在App_Code下 则不需要指定程序集 如下

    <add name="FirstModule" type="CustomServerHeaderModule" />

    注:由于是托管模块 你需要将你的项目部署在IIS中 方有效果 VS中无效


    下来我们可以预览下:

    通过IE调试工具捕获,我们可以很清楚的看到响应标头的变化

    未修改的:

    修改后的:

    好 到此为止吧 希望本文能帮到你~~

  • 相关阅读:
    4-11 EurekaClient集成演示
    4-10 原始版服务调用演示
    4-9 Consumer内容准备
    4-8 Provider内容准备
    Swift:用UICollectionView整一个瀑布流
    Swift: 用Alamofire做http请求,用ObjectMapper解析JSON
    Swift: 用UserDefaults保存复杂对象
    BAT的真的适合创业团队吗?
    为什么要用GCD-Swift2.x
    Objective-C的泛型
  • 原文地址:https://www.cnblogs.com/keanuyaoo/p/3265162.html
Copyright © 2011-2022 走看看