OWIN Katana and Stage Marker in ASP.Net Integrated Pipeline

This is the third article in the series of articles on OWIN and Katana.

  1. Basics of OWIN and Katana
  2. OWIN middleware-ASP.NET integrated Pipeline extensions in Katana

In the previous article of this series we discussed the ASP.NET integrated pipeline and HttpModule. How to add your custom OWIN middleware component? In this article we'll be learning about the various stages of ASP.NET Integrated Pipeline and how to register and run your OMC at a specific stage.

The following is the list of various stages available in the Katana that act as an OWIN middleware:

  1. public enum PipelineStage  
  2. {  
  3.     Authenticate = 0,  
  4.     PostAuthenticate = 1,  
  5.     Authorize = 2,  
  6.     PostAuthorize = 3,  
  7.     ResolveCache = 4,  
  8.     PostResolveCache = 5,  
  9.     MapHandler = 6,  
  10.     PostMapHandler = 7,  
  11.     AcquireState = 8,  
  12.     PostAcquireState = 9,  
  13.     PreHandlerExecute = 10,  

Now if you want to run your code at a specific stage then you just need to register your OMC with the stage. This can be done using StageMarkers in OWIN. So let's create or use our previous OMC and run it at a specific event. In this demo let's use the stage of Authentication and run our OMC during the Authroization stage in the ASP.NET Pipeline.

  1. using AppFunc = Func<IDictionary<stringobject>, Task>;  
  2. using Microsoft.Owin;  
  3.   
  4. public class Startup  
  5. {  
  6.     // Invoked once at startup to configure your application.  
  7.     public void Configuration(IAppBuilder builder)  
  8.     {  
  9.         // Middleware created with in startup class.  
  10.         builder.Use(new Func<AppFunc, AppFunc>(ignoredNextApp => (AppFunc)Invoke));  
  11.         // Specify the stage for the OMC  
  12.         builder.UseStageMarker(PipelineStage.Authenticate);  
  13.    }  
  14.     // Invoked once per request.  
  15.    public Task Invoke(IDictionary<stringobject> environment)  
  16.    {  
  17.        string responseText = "Hello World";  
  18.        // writing response header asynchronously  
  19.        byte[] responseBytes = Encoding.UTF8.GetBytes(responseText);  
  20.       // See http://owin.org/spec/owin-1.0.0.html for standard environment keys.  
  21.       Stream responseStream = (Stream)environment["owin.ResponseBody"];  
  22.       IDictionary<stringstring[]> responseHeaders =  
  23.       (IDictionary<stringstring[]>)environment["owin.ResponseHeaders"];  
  24.       responseHeaders["Content-Length"] = new string[] { responseBytes.Length.ToString(CultureInfo.InvariantCulture) };  
  25.       responseHeaders["Content-Type"] = new string[] { "text/plain" };  
  26.       return responseStream.WriteAsync(responseBytes, 0, responseBytes.Length);  
  27.    }  

Similarly you can something of any of the stages listed above and customize the ASP.NET pipeline to process your request or response.

In our next article we'll be talking about the self-host OWIN process. Say no to System.web and run your web request on an individual server without the boundaries  of a platform or a host/server.


Similar Articles