WebApi OAuth UseOAuthBearerAuthentication gives “Sequence contains more than one element” error

匿名 (未验证) 提交于 2019-12-03 02:03:01

问题:

I configured my WebApi OAuth 2.0 by these lines:

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

But it gives me the following error at each request :

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() 

My OAuthOptions is :

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

If I comment UseOAuthBearerAuthentication everything is ok! I didn't customize OAuthBearerAuthenticationProvider yet and I use it directly but why does it give me error?

回答1:

It should be a bug! Use

app.UseOAuthAuthorizationServer(OAuthOptions);

instead of

app.UseOAuthBearerTokens(OAuthOptions);



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!