• 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调试工具捕获,我们可以很清楚的看到响应标头的变化

    未修改的:

    修改后的:

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

  • 相关阅读:
    VMWare虚拟机下为Ubuntu 12.04.1配置静态IP(NAT方式)
    VMWare虚拟机下为Windows Server 2012配置静态IP(NAT方式)
    Windows 7防火墙阻止了远程桌面连接的解决方法
    Win10系统如何在防火墙里开放端口
    ECharts 定制 label 样式
    目标值柱状图
    echarts中datazoom相关配置
    环形图
    带时间轴的指标监控柱状图
    2020mysql面试题
  • 原文地址:https://www.cnblogs.com/keanuyaoo/p/3265162.html
Copyright © 2020-2023  润新知