How to remove ASP.Net MVC Default HTTP Headers?

前端 未结 11 2117
自闭症患者
自闭症患者 2020-11-28 01:23

Each page in an MVC application I\'m working with sets these HTTP headers in responses:

X-Powered-By: ASP.NET
X-AspNet-Version: 2.0.50727
X-AspNetMvc-Version         


        
相关标签:
11条回答
  • 2020-11-28 01:49

    X-Powered-By is a custom header in IIS. Since IIS 7, you can remove it by adding the following to your web.config:

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

    This header can also be modified to your needs, for more information refer to http://www.iis.net/ConfigReference/system.webServer/httpProtocol/customHeaders


    Add this to web.config to get rid of the X-AspNet-Version header:

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

    Finally, to remove X-AspNetMvc-Version, edit Global.asax.cs and add the following in the Application_Start event:

    protected void Application_Start()
    {
        MvcHandler.DisableMvcResponseHeader = true;
    }
    

    You can also modify headers at runtime via the Application_PreSendRequestHeaders event in Global.asax.cs. This is useful if your header values are dynamic:

    protected void Application_PreSendRequestHeaders(object source, EventArgs e)
    {
          Response.Headers.Remove("foo");
          Response.Headers.Add("bar", "quux");
    }
    
    0 讨论(0)
  • 2020-11-28 01:49

    You can also remove them by adding code to your global.asax file:

     protected void Application_PreSendRequestHeaders(object sender, EventArgs e)
     {
       HttpContext.Current.Response.Headers.Remove("X-Powered-By");
       HttpContext.Current.Response.Headers.Remove("X-AspNet-Version");
       HttpContext.Current.Response.Headers.Remove("X-AspNetMvc-Version");
       HttpContext.Current.Response.Headers.Remove("Server");
     }
    
    0 讨论(0)
  • 2020-11-28 01:49

    You can change any header or anything in Application_EndRequest() try this

    protected void Application_EndRequest()
    {
        // removing excessive headers. They don't need to see this.
        Response.Headers.Remove("header_name");
    }
    
    0 讨论(0)
  • 2020-11-28 01:55

    .NET Core

    To remove the Server header, within the Program.cs file, add the following option:

    .UseKestrel(opt => opt.AddServerHeader = false)
    

    For dot net core 1, put add the option inside the .UseKestrel() call. For dot net core 2, add the line after UseStartup().

    To remove X-Powered-By header, if deployed to IIS, edit your web.config and add the following section inside the system.webServer tag:

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

    .NET 4.5.2

    To remove the Server header, within your global.asax file add the following:

        protected void Application_BeginRequest(object sender, EventArgs e)
        {
            string[] headers = { "Server", "X-AspNet-Version" };
    
            if (!Response.HeadersWritten)
            {
                Response.AddOnSendingHeaders((c) =>
                {
                    if (c != null && c.Response != null && c.Response.Headers != null)
                    {
                        foreach (string header in headers)
                        {
                            if (c.Response.Headers[header] != null)
                            {
                                c.Response.Headers.Remove(header);
                            }
                        }
                    }
                });
            }
    
        }
    

    Pre .NET 4.5.2

    Add the following c# class to your project:

    public class RemoveServerHeaderModule : IHttpModule
    {
        public void Init(HttpApplication context)
        {
            context.PreSendRequestHeaders += OnPreSendRequestHeaders;
        }
    
        public void Dispose() { }
    
        void OnPreSendRequestHeaders(object sender, EventArgs e)
        {
            HttpContext.Current.Response.Headers.Remove("Server");
        }
    }
    

    and then within your web.config add the following <modules> section:

    <system.webServer>
        ....
     <modules>
        <add name="RemoveServerHeaderModule" type="MyNamespace.RemoveServerHeaderModule" />
     </modules>
    

    However I had a problem where sub-projects couldn't find this module. Not fun.

    Removing X-AspNetMvc-Version header

    To remove the ''X-AspNetMvc-Version'' tag, for any version of .NET, modify your ''web.config'' file to include:

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

    Thanks Microsoft for making this unbelievably difficult. Or maybe that was your intention so that you could track IIS and MVC installs across the world ...

    0 讨论(0)
  • 2020-11-28 01:57

    The X-Powered-By header is added by IIS to the HTTP response, so you can remove it even on server level via IIS Manager:

    You can use the web.config directly:

    <system.webServer>
       <httpProtocol>
         <customHeaders>
           <remove name="X-Powered-By" />
         </customHeaders>
       </httpProtocol>
    </system.webServer>
    
    0 讨论(0)
提交回复
热议问题