我有一个不使用 OWIN 中间件的应用程序,并且具有以下内容Global.asax
:
public class MvcApplication : HttpApplication
{
protected void Application_Start()
{
//...
}
protected void Application_PreSendRequestHeaders()
{
Response.Headers.Remove("Server");
}
}
这删除了Server
每次应用程序发送响应时的标头。
如何对使用 OWIN 的应用程序执行相同操作?
public class Startup
{
public void Configuration(IAppBuilder application)
{
//...
}
//What method do I need to create here?
}
您可以创建自己的作品中间件并将其直接注入管道:
public class Startup
{
public void Configuration(IAppBuilder app)
{
app.Use(async (context, next) =>
{
string[] headersToRemove = { "Server" };
foreach (var header in headersToRemove)
{
if (context.Response.Headers.ContainsKey(header))
{
context.Response.Headers.Remove(header);
}
}
await next();
});
}
}
或自定义中间件:
using Microsoft.Owin;
using System.Threading.Tasks;
public class SniffMiddleware : OwinMiddleware
{
public SniffMiddleware(OwinMiddleware next): base(next)
{
}
public async override Task Invoke(IOwinContext context)
{
string[] headersToRemove = { "Server" };
foreach (var header in headersToRemove)
{
if (context.Response.Headers.ContainsKey(header))
{
context.Response.Headers.Remove(header);
}
}
await Next.Invoke(context);
}
}
您可以通过以下方式将其注入管道:
public class Startup
{
public void Configuration(IAppBuilder app)
{
app.Use<SniffMiddleware>();
}
}
不要忘记安装Microsoft.Owin.Host.SystemWeb
:
Install-Package Microsoft.Owin.Host.SystemWeb
否则你的中间件将不会在“IIS集成管道”中执行。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)