Asp.net core uninstall X-Powered-By cannot be performed in middleware

Why can't I uninstall X-Powered-By as part of my middleware that I run? I can remove it if I put it in web.config, but not if I put it in middleware. I delete another header in the Server middleware: Kestrel, which works and tells me that my middleware is running.

I am using Visual Studio 2015, the basic ASP.NET web application (.NET Framework), 1.0.0-rc2-final

My middleware

public class ManageHttpHeadersMiddleware
{
    private RequestDelegate _next;

    public ManageHttpHeadersMiddleware(RequestDelegate next)
    {
        _next = next;
    }

    public async Task Invoke(HttpContext context)
    {
        context.Response.OnStarting(() =>
        {
            context.Response.Headers.Remove("Server");
            context.Response.Headers.Remove("X-Powered-By");

            return Task.CompletedTask;
        });

        await _next(context);
    }
}

My Startup.Configure method looks like

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        loggerFactory.AddSerilog(new LoggerConfiguration()
            .ReadFrom.ConfigurationSection(Configuration.GetSection("Serilog"))
            .CreateLogger())
            .AddDebug();

        app.UseMiddleware<ManageHttpHeadersMiddleware>();

        app.UseJwtBearerAuthentication();

        app.UseMvc();

        app.UseSwaggerGen();
        app.UseSwaggerUi();
    }

So my questions are:

  • Is it because of the order in which I run the intermediate Startup.Configure?
  • - ,    ? OnCompleted, , , "Server": "Kestrel"
  • , Kestrel IIS Azure, web.config?

, , , , .. ..

+4
2

X-Powered-By. , web.config. ( IIS), - -, ASP.net.

ASP.net, ,

  • - IIS - Kestrel (Windows)
  • - NGinx - Kestrel (Linux)

Kestrel, IIS Nginx, , IIS NGInx . X-Powered-By - . web.config IIS.

. , , ASP.net/ASP.net MVC, HTTPModule, . , IIS.

. , Middleware. . ( IIS).

, .

new WebHostBuilder()
    .UseKestrel(c => c.AddServerHeader = false)
+11

X-Powered-By web.config, ​​asp.net

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

Source: https://habr.com/ru/post/1652658/


All Articles