Thursday, March 19, 2015

No OWIN authentication manager is associated with the request

I have recently begun working with OWIN/Katana in order to introduce OAuth2 support to my ASP.NET Web API Applications.

Well, after following some code samples and attempting to browse to my ASP.NET Web API Controller Methods, I received the following error message:



No OWIN authentication manager is associated with the request

On one of my brand new Web API projects, I noticed that I was missing an assembly reference to:

Microsoft.Owin.Host.SystemWeb 

After updating my project to include this NuGet Package reference, my project started working!

Unfortunately, in another instance of my project, I continued encountering this error.  Even worse,

there are very few articles about the root cause of this error when dealing with OWIN/Katana.  In fact, there are still very few articles overall dealing with OWIN/Katana in general.

In any case, I started digging around the project and looking at how OWIN/Katana was doing its processing and as it turns out, Web API was not getting added to the ASP.NET Web API Pipeline!!

I was using some code which was calling an OWIN Module and then immediately setting up the OAuth Server like so:

public void ConfigureAuth(IAppBuilder app)

   {

       

       OAuthOptions = new OAuthAuthorizationServerOptions

       {

           TokenEndpointPath = new PathString("/Token"),

           Provider = new ApplicationOAuthProvider(),

           AccessTokenFormat = new MyJwtFormat(),

           AccessTokenExpireTimeSpan = TimeSpan.FromMinutes(30),

           AllowInsecureHttp = true

       };

 

       app.UseOAuthAuthorizationServer(OAuthOptions);

 

   }


This was somehow truncating the ASP.NET Web API Processing directly out of the OWIN Pipeline, thus resulting in that error message.

As soon as I modified my Startup class and moved the call to setting up the OAuthAuthorizationServer OUTSIDE of the module, my Web API Controller started working again!

This was the final code I was able to get working:




public void ConfigureAuth(IAppBuilder app)

    {

        

        OAuthOptions = new OAuthAuthorizationServerOptions

        {

            TokenEndpointPath = new PathString("/Token"),

            Provider = new ApplicationOAuthProvider(),

            AccessTokenFormat = new MyJwtFormat(),

            AccessTokenExpireTimeSpan = TimeSpan.FromMinutes(30),

            AllowInsecureHttp = true

        };

 

 

 

    }



public partial class Startup

{

    public void Configuration(IAppBuilder app)

    {

        

 

        // Enable the application to use bearer tokens to authenticate users

        ConfigureAuth(app);

        app.UseOAuthAuthorizationServer(OAuthOptions);        

    }

}




No comments:

Post a Comment