How to run Owin Middleware in the IIS integration pipeline, owinmiddleware
Owin Middleware Components (OMCs)
InstallInstall-Package Microsoft.Owin.Host.SystemWeb
Allows OMCs to work under the IIS integration Pipeline
In the IIS integration pipeline, this request pipeline includes HttpModules associated with a set of predefined pipeline events, such
BeginRequest, AuthenticateRequest, AuthorizeRequest, etc.
If we compare OMC with HttpModule, OMC must be registered to an appropriate predefined pipeline event like HttpModule. For example, the following Httpmodule,
When a request comes to the AuthenticateRequest stage,MyModule
Will be called
public class MyModule : IHttpModule{ public void Dispose() { //clean-up code here. } public void Init(HttpApplication context) { // An example of how you can handle AuthenticateRequest events. context.AuthenticateRequest += ctx_AuthRequest; } void ctx_AuthRequest(object sender, EventArgs e) { // Handle event. }}
To enable the OMC to take the same event-based execution sequence as the HttpModule, Katana runs the time code scan Startup configuration and Associates each OMC to an integrated pipeline event,
For example, the following Configuration:
using System;using System.Threading.Tasks;using Microsoft.Owin;using Owin;using System.Web;using System.IO;using Microsoft.Owin.Extensions;[assembly: OwinStartup(typeof(owin2.Startup))]namespace owin2{ public class Startup { public void Configuration(IAppBuilder app) { app.Use((context, next) => { PrintCurrentIntegratedPipelineStage(context, "Middleware 1"); return next.Invoke(); }); app.Use((context, next) => { PrintCurrentIntegratedPipelineStage(context, "2nd MW"); return next.Invoke(); }); app.Run(context => { PrintCurrentIntegratedPipelineStage(context, "3rd MW"); return context.Response.WriteAsync("Hello world"); }); } private void PrintCurrentIntegratedPipelineStage(IOwinContext context, string msg) { var currentIntegratedpipelineStage = HttpContext.Current.CurrentNotification; context.Get<TextWriter>("host.TraceOutput").WriteLine( "Current IIS event: " + currentIntegratedpipelineStage + " Msg: " + msg); } }}
The output is as follows:
Current IIS event: PreExecuteRequestHandler Msg: Middleware 1Current IIS event: PreExecuteRequestHandler Msg: 2nd MWCurrent IIS event: PreExecuteRequestHandler Msg: 3rd MW
We can see that each OMC is mapped to the IIS pipeline event PreRequestHandlerExecute by default during Katana runtime.
You can adjust the default relationship between the OMC and the MPs queue event as needed. Use an extension method.IAppBuilder UseStageMarker(),
As shown below:
public void Configuration(IAppBuilder app){ app.Use((context, next) => { PrintCurrentIntegratedPipelineStage(context, "Middleware 1"); return next.Invoke(); }); app.Use((context, next) => { PrintCurrentIntegratedPipelineStage(context, "2nd MW"); return next.Invoke(); }); app.UseStageMarker(PipelineStage.Authenticate); app.Run(context => { PrintCurrentIntegratedPipelineStage(context, "3rd MW"); return context.Response.WriteAsync("Hello world"); }); app.UseStageMarker(PipelineStage.ResolveCache);}
The output is as follows:
Current IIS event: AuthenticateRequest Msg: Middleware 1Current IIS event: AuthenticateRequest Msg: 2nd MWCurrent IIS event: ResolveRequestCache Msg: 3rd MW
Https://docs.microsoft.com/en-us/aspnet/aspnet/overview/owin-and-katana/owin-middleware-in-the-iis-integrated-pipeline