WebApi OAuth UseOAuthBearerAuthentication给出"Sequence包含多个元素"错误

Mah*_*vej 7 c# oauth-2.0 asp.net-web-api owin bearer-token

我按以下方式配置了我的WebApi OAuth 2.0:

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

    app.UseOAuthBearerTokens(OAuthOptions);
Run Code Online (Sandbox Code Playgroud)

但它在每次请求时都给出了以下错误:

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

OAuthOptions是:

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

如果我评论UseOAuthBearerAuthentication一切都好!我没有自定义OAuthBearerAuthenticationProvider,我直接使用它,但为什么它会给我错误?

Mah*_*vej 36

应该是一个bug!使用

app.UseOAuthAuthorizationServer(OAuthOptions);

代替

app.UseOAuthBearerTokens(OAuthOptions);

  • 你是一把轻剑 (2认同)
  • 你,我的朋友,是乡绅和绅士. (2认同)
  • 你很棒我在最后一天拉你的头发你做我的一天 (2认同)