没有OWIN身份验证管理器与请求关联

Mah*_*vej 66 windows-authentication owin asp.net-mvc-5

在尝试为我的Web Api项目启用owin和AspNet Identity(在VS 2013 + .Net 4.5.1中)后,我在每个有效或无效(请求到无存在的控制器)请求中收到以下错误:

<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>
Run Code Online (Sandbox Code Playgroud)

当我在调试模式下检查时,也没有处理任何异常!我也意识到ConfigurationStartup课堂上永远不会被调用(实际上从未被调试器捕获过).这是启动代码:

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

namespace bloob.bloob
{
    public partial class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            ConfigureAuth(app);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

cal*_*ton 93

我最初使用身份验证创建了项目,但后来决定禁用它.我不得不在WebApiConfig.cs文件中删除它.如果您打算启用身份验证,请确保具有此功能.

        // Web API configuration and services
        // Configure Web API to use only bearer token authentication.
        config.SuppressDefaultHostAuthentication();
        config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));
Run Code Online (Sandbox Code Playgroud)

  • 知道如何关闭身份验证很有用,这确实会使错误消失.但他的意图是启用身份验证,因此这并没有真正回答这个问题. (5认同)
  • 此代码块是为WebAPI配置身份验证的位置.删除它将禁用任何身份验证. (4认同)

Mah*_*vej 91

我终于找到了问题!在逐行比较新创建的项目并发现没有差异之后,我检查了两个项目的参考文献,是的!...所有问题都来自缺少的包:

Microsoft.Owin.Host.SystemWeb
Run Code Online (Sandbox Code Playgroud)

我不知道为什么这个打包在包安装阶段错过了,但奇怪的是,为什么没有抛出任何构建异常?或没有任何DLL引用错误?


小智 8

我的情况,因为web.config中的这个设置失败了.希望这有助于某人避免它.

<appSettings>
    <add key="owin:AutomaticAppStartup" value="false" />
</appSettings>
Run Code Online (Sandbox Code Playgroud)


小智 5

我有同样的问题。该包未出现在 NuGet 包管理器中。我在packages.config中添加了引用:

 <package id="Microsoft.Owin.Host.SystemWeb" version="2.1.0" targetFramework="net45" />
Run Code Online (Sandbox Code Playgroud)

并在项目文件(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>
Run Code Online (Sandbox Code Playgroud)


tom*_*dox 5

将Web.config 中owin:AutomaticAppStartup的密钥更改true为解决了我的问题,即将其更改为:

<appSettings>
    <add key="owin:AutomaticAppStartup" value="false" />
</appSettings>
Run Code Online (Sandbox Code Playgroud)

对此:

<appSettings>
    <add key="owin:AutomaticAppStartup" value="true" />
</appSettings>
Run Code Online (Sandbox Code Playgroud)