OWIN的架构:
Katana当前提东两种server,都支持Windows Integrated Authentication
System.Net.HttpListener
这是Katana的self-host方式的默认选择在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,
}
PreHandlerExecute