Remove Server header (Kestrel)

I’m trying to remove the Server header that is include in the response by default (Server Kestrel)

I found an example of a plugin in a another Thread but it doesn’t seem to work with the Server header.

using ServiceStack;

namespace ServiceStackBase.Plugins
{

   /// <summary>
   /// Remove the Server header.
   /// </summary>
   public class RemoveServerHeaderFeature : IPlugin
   {
       public void Register(IAppHost appHost)
       {
           appHost.Config.GlobalResponseHeaders.Remove("Server");
       }
    }
}

Called in

Configure.AppHost.Configure(Container container) 
{
   Plugins.Add(new RemoveServerHeaderFeature());
 }

The Server header isn’t added by ServiceStack, so can’t be removed in ServiceStack.

Here’s a post showing how to configure the web server to remove it from Startup

With .net 6 and the new WebApplicationBuilder this is the solution if anyone else needs this:

WebApplicationBuilder builder = WebApplication.CreateBuilder(args);

builder.WebHost.UseKestrel(option => option.AddServerHeader = false);

var app = builder.Build();
3 Likes