74

After trying to enable owin & AspNet Identity to my Web Api project (in VS 2013 + .Net 4.5.1) I get the following error in each valid or unvalid(request to none exist controller) requests :

<Error>
<Message>An error has occurred.</Message>
<ExceptionMessage>
No OWIN authentication manager is associated with the request.
</ExceptionMessage>
<ExceptionType>System.InvalidOperationException</ExceptionType>
<StackTrace>
at System.Web.Http.Owin.PassiveAuthenticationMessageHandler.SuppressDefaultAuthenticationChallenges(HttpRequestMessage request) at System.Web.Http.Owin.PassiveAuthenticationMessageHandler.<SendAsync>d__0.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() at System.Web.Http.HttpServer.<SendAsync>d__0.MoveNext()
</StackTrace>
</Error>

As I checked in debug mode, no exception is handled too! Also I realized that Configuration in Startup class is never called (indeed never caught by the debugger). here is the code for startup :

[assembly: OwinStartup(typeof(bloob.bloob.Startup))]

namespace bloob.bloob
{
    public partial class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            ConfigureAuth(app);
        }
    }
}
tomRedox
  • 28,092
  • 24
  • 117
  • 154
Mahmoud Moravej
  • 8,705
  • 6
  • 46
  • 65

9 Answers9

111

I found the problem finally! After comparing line by line with a newly created project and finding no difference , I checked references on both projects and yes!... All the problem was from missing package :

Microsoft.Owin.Host.SystemWeb

I don't know why this packaged is missed in package installation phase but the strange point is that why didn't any build exception thrown? or no any dll reference error?

Mahmoud Moravej
  • 8,705
  • 6
  • 46
  • 65
99

I originally created the project with authentication, but then decided to disable it. I had to remove this in the WebApiConfig.cs file. Make sure you have this if you intend to enable authentication.

        // Web API configuration and services
        // Configure Web API to use only bearer token authentication.
        config.SuppressDefaultHostAuthentication();
        config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));
RajeshKdev
  • 6,365
  • 6
  • 58
  • 80
cal5barton
  • 1,606
  • 1
  • 14
  • 29
  • 4
    This block of code is where authentication is configured for WebAPI. Removing it will disable any authentication. – cal5barton Aug 19 '15 at 19:54
  • If you simply want to turn off all authentication in the webapi pipeline you soould comment out the 3 lines above, worked for me ! – AIDAN CASEY Jan 24 '16 at 15:18
  • hmm, i did this, and added the web.config setting key="owin:AutomaticAppStartup" value="false", but still get this error – Sonic Soul Oct 04 '16 at 15:34
  • 5
    It is useful to know how to turn off authentication, and this does make the error go away. But his intent was to enable authentication, so this does not really answer the question. – Todd Jan 23 '17 at 17:34
9

My case, it failed since this settings in web.config. Hope this helps someone to avoid it.

<appSettings>
    <add key="owin:AutomaticAppStartup" value="false" />
</appSettings>
Vinh Tran
  • 91
  • 1
  • 1
5

I had the same problem. The package was not appearing in the NuGet Package manager. I added reference in packages.config:

 <package id="Microsoft.Owin.Host.SystemWeb" version="2.1.0" targetFramework="net45" />

And reference in the project file (xxx.csproj):

 <Reference Include="Microsoft.Owin.Host.SystemWeb">
  <HintPath>..\packages\Microsoft.Owin.Host.SystemWeb.2.1.0\lib\net45\Microsoft.Owin.Host.SystemWeb.dll</HintPath>
</Reference>
5

Changing the owin:AutomaticAppStartup key to true in Web.config fixed this for me, i.e. change it from:

<appSettings>
    <add key="owin:AutomaticAppStartup" value="false" />
</appSettings>

to this:

<appSettings>
    <add key="owin:AutomaticAppStartup" value="true" />
</appSettings>
tomRedox
  • 28,092
  • 24
  • 117
  • 154
3

if you don't actually need OWIN you can simply uninstall it.

one way to do it is in Nuget Manager uninstall every OWIN library, the order will be dictated by their dependencies.

after this is done you don't need any OWIN related code or config. this worked out best for me since I am using windows auth.

Sonic Soul
  • 23,855
  • 37
  • 130
  • 196
1

I found the problem finally! After comparing line by line with a newly created project and finding no difference , I checked references on both projects and yes!... All the problem was from missing package :

Microsoft.Owin.Host.SystemWeb

Ramdas Chavan
  • 159
  • 1
  • 4
0

For .Net framework adding the runAllManagedModulesForAllRequests attribute to the modules in web.config . This way I kept the authentication exist.

<modules runAllManagedModulesForAllRequests="true">
M. Abouzeid
  • 167
  • 7
-1

If none of the above have helped you can try to reinstall with this command: update-package Microsoft.Owin.Host.SystemWeb -reinstall

Or make sure that owin is actually running with a simple statement in the Startup class

For me it helped to add the assembly the in web.config:

<system.web>
    <compilation debug="true" targetFramework="4.5.2" batch="false" optimizeCompilations="true" defaultLanguage="C#">
       <assemblies>
           <add assembly="Microsoft.Owin.Host.SystemWeb" />
       </assemblies>
T.Marq
  • 1
  • 1