zoukankan      html  css  js  c++  java
  • ASP.NET中指定自定义HTTP响应标头

    新建一个类HideServerHeaderHelper,继承 IHttpModule,然后重写 OnPreSendRequestHeaders,Dispose,Init方法,如下代码所示

    using System;
    using System.Collections.Generic;
    using System.Web;
    
    namespace MvcApp.Filters
    {
        public class HideServerHeaderHelper : IHttpModule
        {
            /// <summary>
            /// List of Headers to remove
            /// </summary>
            private readonly List<string> _headersToCloak;
    
            /// <summary>
            /// Initializes a new instance of the <see cref="HideServerHeaderModule"/> class.
            /// </summary>
            public HideServerHeaderHelper()
            {
                _headersToCloak = new List<string>
                                          {
                                                  "Server",
                                                  "X-AspNet-Version",
                                                  "X-AspNetMvc-Version",
                                                  "X-Powered-By"
                                          };
            }
    
            private void OnPreSendRequestHeaders(object sender, EventArgs e)
            {
                //设置Server的值
    
                _headersToCloak.ForEach(h => HttpContext.Current.Response.Headers.Remove(h));
                HttpContext.Current.Response.Headers.Set("Server", "nginx");
                HttpContext.Current.Response.Headers.Set("Powered-By", "东讯科技有限公司");
            }
    
            public void Dispose()
            {
                _headersToCloak.Clear();
            }
    
            public void Init(HttpApplication context)
            {
                context.PreSendRequestHeaders += OnPreSendRequestHeaders;
            }
        }
    }

    在web.config添加以下模块

    <system.webServer>
        <modules runAllManagedModulesForAllRequests="true">
          <add name="HideServerHeaderModule" type="MvcApp.Filters.HideServerHeaderModule"/>
        </modules>
    
     <httpProtocol>
          <customHeaders>
            <remove name="Server"/>
            <remove name="X-AspNet-Version"/>
            <remove name="X-AspNetMvc-Version"/>
            <remove name="X-Powered-By"/>
          </customHeaders>
        </httpProtocol>
      </system.webServer>
  • 相关阅读:
    Windows性能计数器应用
    Azure Oracle Linux VNC 配置
    Azure 配置管理系列 Oracle Linux (PART6)
    Azure 配置管理系列 Oracle Linux (PART5)
    Azure 配置管理系列 Oracle Linux (PART4)
    Azure 配置管理系列 Oracle Linux (PART3)
    Azure 配置管理系列 Oracle Linux (PART2)
    vagrant多节点配置
    docker基本操作
    LINUX开启允许对外访问的网络端口命令
  • 原文地址:https://www.cnblogs.com/david1989/p/3674423.html
Copyright © 2011-2022 走看看