This is not a big problem, but is there any way to disable, hide or change the Server header in the HTTP response? The Server property is read-only in the Headers collection of the HttpResponseMessage, and it always returns "Microsoft-HTTPAPI/2.0".
Except the OP said he is running self-host, and IHTTPModules are an ASP.NET Runtime thing. I think that header gets added in the HTTPListener class and I don't know if there is any way of getting to that.
Except the OP said he is running self-host, and IHTTPModules are an ASP.NET Runtime thing. I think that header gets added in the HTTPListener class and I don't know if there is any way of getting to that.
Er, sorry, just glazed right on over that key point. Long day of porting to .NET 4.5/MVC 4/Azure 1.7... apologies... :D
richmiller
Member
40 Points
51 Posts
Hiding or Changing the Server Header in the Web API Response
Aug 17, 2012 07:31 PM|LINK
This is not a big problem, but is there any way to disable, hide or change the Server header in the HTTP response? The Server property is read-only in the Headers collection of the HttpResponseMessage, and it always returns "Microsoft-HTTPAPI/2.0".
Sorry, failed to mention this is self-hosted.
Thanks.
mitja.GTI
Star
11157 Points
2094 Posts
Re: Hiding or Changing the Server Header in the Web API Response
Aug 19, 2012 12:26 AM|LINK
I'm not sure if it will work since I am more of a WCF guy but have you tried WebOperationContext?
mitja.gti | www.mitjagti.com
Darrel Mille...
Member
164 Points
55 Posts
Re: Hiding or Changing the Server Header in the Web API Response
Aug 19, 2012 09:15 PM|LINK
You can add a new server value to the header, but I haven't found a way to prevent the framework from adding it's product name there...
response.Headers.Server.Add(new ProductInfoHeaderValue("MyCoolServer", "1.0"));DPeden
Member
14 Points
15 Posts
Re: Hiding or Changing the Server Header in the Web API Response
Aug 20, 2012 12:47 AM|LINK
You can run a custom module that does it:
public class ResponseHeaderRemoverHttpModule : IHttpModule { private static readonly string[] headers = new[] { "Server", "X-AspNet-Version", "X-Powered-By" }; public void Init(HttpApplication context) { context.PreSendRequestHeaders += (sender, eventArgs) => headers.ForEach(header => context.Response.Headers.Remove(header)); } public void Dispose() { } }Note: the .ForEach is a custom extension method. Should be easy to implement or change as you see fit.
Darrel Mille...
Member
164 Points
55 Posts
Re: Hiding or Changing the Server Header in the Web API Response
Aug 20, 2012 12:50 AM|LINK
Except the OP said he is running self-host, and IHTTPModules are an ASP.NET Runtime thing. I think that header gets added in the HTTPListener class and I don't know if there is any way of getting to that.
DPeden
Member
14 Points
15 Posts
Re: Hiding or Changing the Server Header in the Web API Response
Aug 20, 2012 12:52 AM|LINK
Er, sorry, just glazed right on over that key point. Long day of porting to .NET 4.5/MVC 4/Azure 1.7... apologies... :D