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

时间:2016-02-23 17:31:16

标签: c# asp.net asp.net-mvc asp.net-web-api2 owin

我有一个简单的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>
  ...

1 个答案:

答案 0 :(得分:1)

HttpContext.Current.CurrentNotificationRequestNotification的枚举类型。根据{{​​3}},它只有一个与authenticate相关的值:AuthenticateRequest,我认为它包括PrePostAuthenticate,PostAuthenticate和PostPostAuthenticate阶段。

RequestNotification值列表:

  • AcquireRequestState
  • AuthenticateRequest
  • AuthorizeRequest
  • BeginRequest
  • EndRequest
  • ExecuteRequestHandler
  • LogRequest
  • MapRequestHandler
  • PreExecuteRequestHandler
  • ReleaseRequestState
  • ResolveRequestCache
  • SendResponse
  • UpdateRequestCache