WebApi OAuth 使用UseOAuthBearerAuthentication会出现“Sequence contains more than one element”错误。

8

我通过以下代码配置了我的 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,
    };
}

如果我注释掉UseOAuthBearerAuthentication,一切都正常!但是我还没有自定义OAuthBearerAuthenticationProvider,我直接使用它,为什么会出错?
1个回答

38

这应该是一个bug!使用

app.UseOAuthAuthorizationServer(OAuthOptions);

而不是

app.UseOAuthBearerTokens(OAuthOptions);


2
你是一把光剑。 - Kunal B.
嗨,我遇到了同样的问题。这个之前是可以用的,但现在不行了。我找到了一些线索,可能的原因是从3.0.0升级到3.0.1的Owin版本。然而,即使我注册了3.0.0,它仍然不能用。尝试了上面的解决方案,但还是不行。有人能指导一下吗? - Abi P
2
你很棒,我在过去的一天里都快抓狂了,但是你让我的一天变得美好。 - DKR
1
@MahmoudMoravej - 好吧,我花了一整天的时间…有人能解释一下为什么这个可以工作吗? - Diskdrive
1
这节省了我的查找时间。 - Stanley Okpala Nwosa
显示剩余3条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接