net example bearer asp c# asp.net-web-api oauth-2.0 owin bearer-token

c# - example - web api add authentication



WebApi OAuth UseOAuthBearerAuthentication muestra el error "La secuencia contiene más de un elemento" (1)

¡Debería ser un error! Utilizar

app.UseOAuthAuthorizationServer(OAuthOptions);

en lugar de

app.UseOAuthBearerTokens(OAuthOptions);

Configuré mi WebApi OAuth 2.0 por estas líneas:

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

Pero me da el siguiente error en cada solicitud:

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

Mis OAuthOptions es:

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

Si comento UseOAuthBearerAuthentication ¡ todo está bien! No he personalizado OAuthBearerAuthenticationProvider todavía y lo uso directamente, pero ¿por qué me da error?