C# WebApi OAuth使用OAuthBeareAuthentication给出;序列包含多个元素;错误

C# WebApi OAuth使用OAuthBeareAuthentication给出;序列包含多个元素;错误,c#,asp.net-web-api,oauth-2.0,owin,bearer-token,C#,Asp.net Web Api,Oauth 2.0,Owin,Bearer Token,我通过以下几行代码配置了WebApi OAuth 2.0: app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions { Provider = new OAuthBearerAuthenticationProvider(), }); app.UseOAuthBearerTokens(OAuthOptions); 但每次请求时,它都会给我以下错误: Message

我通过以下几行代码配置了WebApi OAuth 2.0:

    app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions
    {
        Provider = new OAuthBearerAuthenticationProvider(),
    });

    app.UseOAuthBearerTokens(OAuthOptions);
但每次请求时,它都会给我以下错误:

Message : An error has occurred.
ExceptionMessage : Sequence contains more than one element
ExceptionType : System.InvalidOperationException
StackTrace :    at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source)
   at Microsoft.Owin.Security.AuthenticationManager.d__8.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.HostAuthenticationFilter.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.Controllers.AuthenticationFilterResult.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.Dispatcher.HttpControllerDispatcher.d__1.MoveNext()
我的
OAuthOptions
是:

    OAuthOptions = new OAuthAuthorizationServerOptions
    {
        TokenEndpointPath = new PathString("/Token"),
        Provider = new ApplicationOAuthProvider(PublicClientId, UserManagerFactory),
        AuthorizeEndpointPath = new PathString("/Account/ExternalLogin"),
        AccessTokenExpireTimeSpan = TimeSpan.FromDays(14),
        AllowInsecureHttp = true,
    };
}

如果我评论使用OAuthBeareAuthentication一切正常!我还没有自定义OAuthBeareAuthenticationProvider,我直接使用它,但为什么它会给我错误?

这应该是一个bug!使用

app.useAuthAuthorizationServer(OAuthOptions)

而不是


app.useAuthBearTokens(OAuthOptions)

你是一把光剑,我也有同样的问题。这对我有用,但现在不再有用了。我确实发现一些线程认为可能的原因是Owin版本从3.0.0升级到3.0.1。然而,即使我注册了3.0.0,它对我也不起作用。尝试了上述解决方案,但仍然无效。这里有导游吗?你太棒了,我上一天拉头发,你让我day@MahmoudMoravej-好吧,这花了我大约一天的时间。。。有人能解释一下为什么这样做吗?这节省了我的查找时间。