Application_PreSendRequestHeaders () sur OWIN

J’ai une application qui n’utilise pas le middleware OWIN et qui a le Global.asax suivant:

 public class MvcApplication : HttpApplication { protected void Application_Start() { //... } protected void Application_PreSendRequestHeaders() { Response.Headers.Remove("Server"); } } 

Cela supprime l’en-tête du Server chaque fois que l’application envoie une réponse.

Comment puis-je faire la même chose avec une application qui utilise OWIN?

 public class Startup { public void Configuration(IAppBuilder application) { //... } //What method do I need to create here? } 

Vous pouvez enregistrer un rappel pour l’événement IOwinResponse.OnSendingHeaders :

 public class Startup { public void Configuration(IAppBuilder app) { app.Use(async (context, next) => { context.Response.OnSendingHeaders(state => { ((OwinResponse)state).Headers.Remove("Server"); }, context.Response); await next(); }); // Configure the rest of your application... } } 

Vous pouvez créer votre propre logiciel intermédiaire et l’injecter directement dans le pipeline:

 public class Startup { public void Configuration(IAppBuilder app) { app.Use(async (context, next) => { ssortingng[] headersToRemove = { "Server" }; foreach (var header in headersToRemove) { if (context.Response.Headers.ContainsKey(header)) { context.Response.Headers.Remove(header); } } await next(); }); } } 

ou un middleware personnalisé:

 using Microsoft.Owin; using System.Threading.Tasks; public class SniffMiddleware : OwinMiddleware { public SniffMiddleware(OwinMiddleware next): base(next) { } public async override Task Invoke(IOwinContext context) { ssortingng[] headersToRemove = { "Server" }; foreach (var header in headersToRemove) { if (context.Response.Headers.ContainsKey(header)) { context.Response.Headers.Remove(header); } } await Next.Invoke(context); } } 

que vous pouvez injecter dans le pipeline de cette façon:

 public class Startup { public void Configuration(IAppBuilder app) { app.Use(); } } 

N’oubliez pas d’installer Microsoft.Owin.Host.SystemWeb :

 Install-Package Microsoft.Owin.Host.SystemWeb 

ou votre middleware ne sera pas exécuté dans le “pipeline intégré IIS”.