【问题标题】:WebApi OAuth UseOAuthBearerAuthentication gives "Sequence contains more than one element" errorWebApi OAuth UseOAuthBearerAuthentication 给出“序列包含多个元素”错误
【发布时间】:2014-09-18 16:16:42
【问题描述】:

我通过以下几行配置了我的 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,直接使用了,为什么会报错?

【问题讨论】:

    标签: c# asp.net-web-api oauth-2.0 owin bearer-token


    【解决方案1】:

    这应该是一个错误!使用

    app.UseOAuthAuthorizationServer(OAuthOptions);

    而不是

    app.UseOAuthBearerTokens(OAuthOptions);

    【讨论】:

    • 你是光剑
    • 您好,我遇到了同样的问题。这对我有用,但现在它不再起作用了。我确实发现了一些暗示可能的原因是 Owin 版本从 3.0.0 升级到 3.0.1 的线程。但是,即使我注册了 3.0.0,它对我也不起作用。试过上面的解决方法还是不行。有人可以在这里指导吗?
    • 你太棒了,我最后一天拉了我的头发,你让我开心
    • @MahmoudMoravej - 好吧,这花了我大约一天的时间......有人能解释一下为什么会这样吗?
    • 这节省了我的查找时间。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-11-26
    • 2017-11-21
    • 1970-01-01
    • 2015-01-21
    • 2014-03-09
    • 2013-02-07
    • 1970-01-01
    相关资源
    最近更新 更多