Why can I not remove X-Powered-By as part of my middleware that I am executing? I can remove it if I put in the web.config but not if I put it in the middleware. I am removing another header in the middleware "Server" : "Kestrel" which works and tells me my middleware is being executed.
I am using Visual Studio 2015, ASP.Net Core 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 this
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 am executing the middleware in the Startup.Configure ?
- Is it because of the event I am executing in the middleware ? I have tried using OnCompleted but its obviously to late and does not then remove "Server" : "Kestrel"
- Is it because its added by Kestrel or IIS in Azure and the only way to remove is via the web.config ?
I know that you could argue I have a work around and what's my problem, but it would be nice to achieve the same requirement in the same code location, to help maintainability, etc, etc.