hutchonoid
hutchonoid

Reputation: 33306

OWIN middleware PostAuthenticate event never fires

I have a simple example of OWIN middleware and I want to tap into the PipelineStage.PostAuthenticate stage via:

app.UseStageMarker(PipelineStage.PostAuthenticate);

The problem I have is that it never seems to hit the PostAuthenticateRequest event, even though the user is authenticated.

It always prints:

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

I can work around this by using the events in Global.asax or an IHttpModule but I would rather use the OWIN Pipleine.

Simple example:

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

Taken from: OWIN Middleware in the IIS integrated pipeline

The app is configured to use windows:

Anonymous Authentication = false

Windows Authentication = true

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

Upvotes: 1

Views: 1861

Answers (1)

Stephen Zeng
Stephen Zeng

Reputation: 2818

HttpContext.Current.CurrentNotification is an enum type of RequestNotification. According to https://msdn.microsoft.com/en-us/library/system.web.requestnotification(v=vs.110).aspx, it only has one value related to authenticate: AuthenticateRequest, which I believe includes PrePostAuthenticate, PostAuthenticate and PostPostAuthenticate stages.

List of RequestNotification values:

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

Upvotes: 1

Related Questions