Elma reveals: "There is no OWIN authentication manager associated with the request."

My elmah logs show this error. This does not happen with every request. This happens intermittently. MVC 5.2, Web API 2.2, ASP.NET Identity 2, and Azure are my environment. update: Here is my Startup.cs,

OAuthOptions = new OAuthAuthorizationServerOptions() { AllowInsecureHttp = true, TokenEndpointPath = new PathString("/token"), AccessTokenExpireTimeSpan = TimeSpan.FromMinutes(settings.AccessTokenExpireTimeSpanInMinutes), Provider = new MyAuthorizationServerProvider(), RefreshTokenProvider = new MyRefreshTokenProvider() }; // Web API configuration and services // Configure Web API to use only bearer token authentication. GlobalConfiguration.Configuration.SuppressDefaultHostAuthentication(); GlobalConfiguration.Configuration.Filters.Add(new HostAuthenticationFilter(OAuthOptions.AuthenticationType)); app.UseOAuthAuthorizationServer(OAuthOptions); app.UseOAuthBearerAuthentication(OAuthBearerOptions); GlobalConfiguration.Configuration.SuppressDefaultHostAuthentication(); GlobalConfiguration.Configuration.Filters.Add(new HostAuthenticationFilter(OAuthOptions.AuthenticationType)); 

Checked this. There is no OWIN authentication manager associated with the request , but it does not work.

 System.InvalidOperationException: No OWIN authentication manager is associated with the request. at System.Web.Http.HostAuthenticationFilter.GetAuthenticationManagerOrThrow(HttpRequestMessage request) at System.Web.Http.HostAuthenticationFilter.<AuthenticateAsync>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.Web.Http.Tracing.ITraceWriterExtensions.<TraceBeginEndAsyncCore>d__24.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.Web.Http.Controllers.AuthenticationFilterResult.<ExecuteAsync>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.Web.Http.Tracing.Tracers.HttpControllerTracer.<ExecuteAsyncCore>d__5.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.Web.Http.Tracing.ITraceWriterExtensions.<TraceBeginEndAsyncCore>d__18`1.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.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__0.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Web.Http.Dispatcher.HttpControllerDispatcher.<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.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.Web.Http.Tracing.ITraceWriterExtensions.<TraceBeginEndAsyncCore>d__18`1.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.Web.Http.Tracing.ITraceWriterExtensions.<TraceBeginEndAsyncCore>d__18`1.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.Web.Http.HttpServer.<SendAsync>d__0.MoveNext() 

I was able to play it on my pc. Just restart the application domain and send the request to api. I will get the above error. After that, everything will be fine. Here is the stacktrace from my local machine,

  Project.Api.dll!Project.Api.Helpers.ElmahExceptionHandler.Handle(System.Web.Http.ExceptionHandling.ExceptionHandlerContext context) Line 22 C# System.Web.Http.dll!System.Web.Http.ExceptionHandling.ExceptionHandler.HandleAsync(System.Web.Http.ExceptionHandling.ExceptionHandlerContext context, System.Threading.CancellationToken cancellationToken) Unknown System.Web.Http.dll!System.Web.Http.ExceptionHandling.ExceptionHandler.System.Web.Http.ExceptionHandling.IExceptionHandler.HandleAsync(System.Web.Http.ExceptionHandling.ExceptionHandlerContext context, System.Threading.CancellationToken cancellationToken) Unknown System.Web.Http.dll!System.Web.Http.ExceptionHandling.LastChanceExceptionHandler.HandleAsync(System.Web.Http.ExceptionHandling.ExceptionHandlerContext context, System.Threading.CancellationToken cancellationToken) Unknown System.Web.Http.dll!System.Web.Http.ExceptionHandling.ExceptionHandlerExtensions.HandleAsyncCore() Unknown mscorlib.dll!System.Runtime.CompilerServices.AsyncMethodBuilderCore.Start<System.Web.Http.ExceptionHandling.ExceptionHandlerExtensions.HandleAsyncCore>(ref System.Web.Http.ExceptionHandling.ExceptionHandlerExtensions.HandleAsyncCore stateMachine) Unknown mscorlib.dll!System.Runtime.CompilerServices.AsyncTaskMethodBuilder<System.__Canon>.Start<System.Web.Http.ExceptionHandling.ExceptionHandlerExtensions.HandleAsyncCore>(ref System.Web.Http.ExceptionHandling.ExceptionHandlerExtensions.HandleAsyncCore stateMachine) Unknown System.Web.Http.dll!System.Web.Http.ExceptionHandling.ExceptionHandlerExtensions.HandleAsyncCore(System.Web.Http.ExceptionHandling.IExceptionHandler handler, System.Web.Http.ExceptionHandling.ExceptionHandlerContext context, System.Threading.CancellationToken cancellationToken) Unknown System.Web.Http.dll!System.Web.Http.ExceptionHandling.ExceptionHandlerExtensions.HandleAsync(System.Web.Http.ExceptionHandling.IExceptionHandler handler, System.Web.Http.ExceptionHandling.ExceptionContext context, System.Threading.CancellationToken cancellationToken) Unknown System.Web.Http.dll!System.Web.Http.HttpServer.SendAsync() Unknown mscorlib.dll!System.Runtime.CompilerServices.AsyncMethodBuilderCore.Start<System.Web.Http.HttpServer.SendAsync>(ref System.Web.Http.HttpServer.SendAsync stateMachine) Unknown mscorlib.dll!System.Runtime.CompilerServices.AsyncTaskMethodBuilder<System.__Canon>.Start<System.Web.Http.HttpServer.SendAsync>(ref System.Web.Http.HttpServer.SendAsync stateMachine) Unknown System.Web.Http.dll!System.Web.Http.HttpServer.SendAsync(System.Net.Http.HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) Unknown System.Net.Http.dll!System.Net.Http.HttpMessageInvoker.SendAsync(System.Net.Http.HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) Unknown System.Web.Http.WebHost.dll!System.Web.Http.WebHost.HttpControllerHandler.ProcessRequestAsyncCore() Unknown mscorlib.dll!System.Runtime.CompilerServices.AsyncMethodBuilderCore.Start<System.Web.Http.WebHost.HttpControllerHandler.ProcessRequestAsyncCore>(ref System.Web.Http.WebHost.HttpControllerHandler.ProcessRequestAsyncCore stateMachine) Unknown mscorlib.dll!System.Runtime.CompilerServices.AsyncTaskMethodBuilder.Start<System.Web.Http.WebHost.HttpControllerHandler.ProcessRequestAsyncCore>(ref System.Web.Http.WebHost.HttpControllerHandler.ProcessRequestAsyncCore stateMachine) Unknown System.Web.Http.WebHost.dll!System.Web.Http.WebHost.HttpControllerHandler.ProcessRequestAsyncCore(System.Web.HttpContextBase contextBase) Unknown System.Web.Http.WebHost.dll!System.Web.Http.WebHost.HttpControllerHandler.ProcessRequestAsync(System.Web.HttpContext context) Unknown System.Web.dll!System.Web.HttpTaskAsyncHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest.AnonymousMethod__0() Unknown System.Web.dll!System.Web.TaskAsyncHelper.BeginTask(System.Func<System.Threading.Tasks.Task> taskFunc, System.AsyncCallback callback, object state) Unknown System.Web.dll!System.Web.HttpTaskAsyncHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(System.Web.HttpContext context, System.AsyncCallback cb, object extraData) Unknown System.Web.dll!System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() Unknown System.Web.dll!System.Web.HttpApplication.ExecuteStep(System.Web.HttpApplication.IExecutionStep step, ref bool completedSynchronously) Unknown System.Web.dll!System.Web.HttpApplication.PipelineStepManager.ResumeSteps(System.Exception error) Unknown System.Web.dll!System.Web.HttpApplication.BeginProcessRequestNotification(System.Web.HttpContext context, System.AsyncCallback cb) Unknown System.Web.dll!System.Web.HttpRuntime.ProcessRequestNotificationPrivate(System.Web.Hosting.IIS7WorkerRequest wr, System.Web.HttpContext context) Unknown System.Web.dll!System.Web.Hosting.PipelineRuntime.ProcessRequestNotificationHelper(System.IntPtr rootedObjectsPointer, System.IntPtr nativeRequestContext, System.IntPtr moduleData, int flags) Unknown System.Web.dll!System.Web.Hosting.PipelineRuntime.ProcessRequestNotification(System.IntPtr rootedObjectsPointer, System.IntPtr nativeRequestContext, System.IntPtr moduleData, int flags) Unknown [Native to Managed Transition] [Managed to Native Transition] System.Web.dll!System.Web.Hosting.PipelineRuntime.ProcessRequestNotificationHelper(System.IntPtr rootedObjectsPointer, System.IntPtr nativeRequestContext, System.IntPtr moduleData, int flags) Unknown System.Web.dll!System.Web.Hosting.PipelineRuntime.ProcessRequestNotification(System.IntPtr rootedObjectsPointer, System.IntPtr nativeRequestContext, System.IntPtr moduleData, int flags) Unknown [AppDomain Transition] 

I do not use the OAuth server, which is available in the SPA template.

 config.SuppressDefaultHostAuthentication(); config.Filters.Add(new HostAuthenticationFilter(Startup.OAuthOptions.AuthenticationType)); 
+5
source share
3 answers

The fix is ​​to use Microsoft.AspNet.WebApi.Owin (UseOwinMethod). This also fixed Server cannot append header after HTTP headers have been sent. . I found an error in this package that could also be work around. Why does MessageHandler execute an MVC request? Related http://katanaproject.codeplex.com/discussions/540202

+1
source

I use a third-party basic authentication Thinktecture.IdentityModel.Owin.BasicAuthentication ( Thinktecture.IdentityModel.Owin.BasicAuthentication ) and get the same error as the OP. I always had a link to Microsoft.Owin.Host.SystemWeb .

In the end, I skipped these lines in my App_Start code:

 config.SupressDefaultHostAuthentication(); config.Filters.Add( new HostAuthenticationFilter("") ); 

After I deleted both lines, it worked as intended.

+1
source

How are you trying to access the owin context in your API? In WebApi you should use Request.GetOwinContext () (unlike HttpContext.GetOwinContext (), which you would use in MVC).

0
source

Source: https://habr.com/ru/post/1202970/


All Articles