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

时间:2014-07-27 07:44:31

标签: 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 : 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 个答案:

答案 0 :(得分:34)

应该是一个错误!使用

app.UseOAuthAuthorizationServer(OAuthOptions);

而不是

app.UseOAuthBearerTokens(OAuthOptions);