在IIS集成管道中使用OWIN Middleware

在Katana中启用Windows Authorization

OWIN的架构:

  • Host 管理OWIN pipeline上运行的进程
  • Server 打开一个network socket,,监听请求
  • Middleware 处理HTTP请求并响应

Katana当前提东两种server,都支持Windows Integrated Authentication

  • Microsoft.Owin.Host.SystemWeb 使用IIS的asp.net pipeline
  • Microsoft.Owin.Host.HttpListener 使用System.Net.HttpListener这是Katana的self-host方式的默认选择

在IIS集成管道中使用OWIN Middleware

在IIS集成管道中使用OWIN中间件,,通过将中间件注册到相应的pipeline event

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);
        }
    }
}

OWIN middleware Components (OMCs)

IAppBuilder UseStageMarker()可以使用设置OMCs在指定的pipeline阶段执行

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);
}

app.UseStageMarker(PipelineStage.Authenticate)使所有此处前面注册的中间件,在authentication阶段执行

OWIN Middleware component(OMC)可以一些阶段:

public enum PipelineStage
{
    Authenticate = 0,
    PostAuthenticate = 1,
    Authorize = 2,
    PostAuthorize = 3,
    ResolveCache = 4,
    PostResolveCache = 5,
    MapHandler = 6,
    PostMapHandler = 7,
    AcquireState = 8,
    PostAcquireState = 9,
    PreHandlerExecute = 10,
}
  1. 默认,OMCs运行在PreHandlerExecute
  2. OWIN pipeline和IIS pipeline是有顺序的。现在设置的pipeline阶段不能早于之后设置的pipeline阶段。否则都将在后一个设置的阶段执行。

你可能感兴趣的:(IIS)