8

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?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Mahmoud Moravej
  • 8,705
  • 6
  • 46
  • 65

1 Answers1

38

It should be a bug! Use

app.UseOAuthAuthorizationServer(OAuthOptions);

instead of

app.UseOAuthBearerTokens(OAuthOptions);

Mahmoud Moravej
  • 8,705
  • 6
  • 46
  • 65
  • 2
    You are a light-saber – Kunal B. Jul 18 '15 at 04:50
  • Hi, I am having the same issue. This was working for me but now it does not work any more. I did find some threads that suggested possible cause is Owin version updrage from 3.0.0 to 3.0.1. However even if I registered 3.0.0 it does not work for me. Tried the above solution still it does not work. Could any one guide here? – Abi P Sep 01 '15 at 18:54
  • Ask your question in a new thread. No one can see your question here! – Mahmoud Moravej Sep 01 '15 at 19:01
  • 2
    you are great i pull my hair last 1 day you make my day – DKR Feb 17 '16 at 04:36
  • 1
    @MahmoudMoravej - Ok this cost me about a day... can someone explain why does this work? – Diskdrive Nov 21 '17 at 12:24
  • 1
    This saved my finding time. – Stanley Okpala Nwosa Mar 08 '18 at 10:05
  • If someone came here because of two-factor authentication so what solved my issue was just to delete all the bin folder. Like here: https://stackoverflow.com/questions/10986909/a-route-named-x-is-already-in-the-route-collection-route-names-must-be-unique – The scion Feb 04 '19 at 14:05
  • I faced the similar issue in one of our project, It took sometime to solve, but I find out that UseIdentityServerBearerTokenAuthentication was used twice. Removing one solved my problem – AstroBoy Aug 10 '20 at 12:13