OWIN 中间件 PostAuthenticate 事件永远不会触发

OWIN middleware PostAuthenticate event never fires

我有一个简单的 OWIN 中间件示例,我想通过以下方式进入 PipelineStage.PostAuthenticate 阶段:

app.UseStageMarker(PipelineStage.PostAuthenticate);

我遇到的问题是它似乎从未触发 PostAuthenticateRequest 事件,即使用户已通过身份验证。

它总是打印:

Current IIS event: AuthenticateRequest Msg: Should be Auth
Current IIS event: AuthenticateRequest Msg: Should be PostAuth

我可以通过使用 Global.asaxIHttpModule 中的事件来解决这个问题,但我宁愿使用 OWIN Pipleine。

简单例子:

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, "Should be Auth");
                return next.Invoke();
            });
            app.UseStageMarker(PipelineStage.Authenticate);
            app.Use((context, next) =>
            {
                PrintCurrentIntegratedPipelineStage(context, "Should be PostAuth");
                return next.Invoke();
            });
            app.UseStageMarker(PipelineStage.PostAuthenticate);

        }
        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 in the IIS integrated pipeline

该应用配置为使用 windows:

匿名身份验证 = false

Windows 身份验证 = true

<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.5.2"/>
    <httpRuntime targetFramework="4.5.2"/>
    <authentication mode="Windows"></authentication>
  </system.web>
  ...

HttpContext.Current.CurrentNotificationRequestNotification 的枚举类型。根据https://msdn.microsoft.com/en-us/library/system.web.requestnotification(v=vs.110).aspx,它只有一个与认证相关的值:AuthenticateRequest,我认为包括PrePostAuthenticate,PostAuthenticate和PostPostAuthenticate阶段。

RequestNotification 值列表:

  • 获取请求状态
  • AuthenticateRequest
  • 授权请求
  • 开始请求
  • 结束请求
  • 执行请求处理程序
  • 日志请求
  • MapRequestHandler
  • PreExecuteRequestHandler
  • ReleaseRequestState
  • 解析请求缓存
  • 发送响应
  • 更新请求缓存