The sequence contains more than one item. Microsoft.Owin.Security.AuthenticationManager
When trying to authenticate from the outside with Google, the app gives me the following exception:
<Error> <Message> An error has occurred. <ExceptionMessage> Sequence contains more than one element </ExceptionMessage> <ExceptionType> System.InvalidOperationException </ExceptionType> <StackTrace> in System.Linq.Enumerable.SingleOrDefault [TSource] (IEnumerable
1 source) at Microsoft.Owin.Security.AuthenticationManager.<AuthenticateAsync>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 the stack trace from the previous place where the exception was thrown - - at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (task task) at System.Runtime.CompilerServices.TaskAwaiter.GetResult () at System.Web.Http.Controllers.AuthenticationFilterResult.d__0.MoveNext location from previous trace where the exception was thrown - at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (Task Task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (Task Task) at System.Runtime.CompilerServices.Task .Web.Http.Dispatcher.HttpControllerDispatcher.d__1.MoveNext ()
I have configured my Web Api oAuth like this:
public void ConfigureOAuth(IAppBuilder app)
{
app.UseExternalSignInCookie(
Microsoft.AspNet.Identity.DefaultAuthenticationTypes.ExternalCookie);
OAuthBearerOptions = new OAuthBearerAuthenticationOptions();
OAuthAuthorizationServerOptions OAuthServerOptions =
new OAuthAuthorizationServerOptions()
{
AllowInsecureHttp = true,
TokenEndpointPath = new PathString("/token"),
AccessTokenExpireTimeSpan = TimeSpan.FromMinutes(30),
Provider = new SimpleAuthorizationServerProvider(),
};
app.UseOAuthAuthorizationServer(OAuthServerOptions);
app.UseOAuthBearerAuthentication(OAuthBearerOptions);
googleAuthOptions = new GoogleOAuth2AuthenticationOptions()
{
ClientId = ClientId,
ClientSecret = ClientSecret,
Provider = new GoogleAuthProvider()
};
app.UseGoogleAuthentication(googleAuthOptions);
}
source to share
I also got this error and it turned out that I had the following:
app.UseCookieAuthentication(new CookieAuthenticationOptions
{AuthenticationType = DefaultAuthenticationTypes.ExternalCookie}
in my SecurityConfig but also this on the controller:
[HostAuthentication(DefaultAuthenticationTypes.ExternalCookie)]
(For completeness: I only used web.api owin using owin IIS hosting)
Solution: Removing one of these fixed issues, so I'm guessing adding such a controller attribute is similar to setting it twice. Look for suspicious things like this (they can also be configured in the library you are using!)
More information on where the error occurs:
In my case, it was noticed that many times (but not always ?!) an error occurs. Debug found out that the offending method is in Microsoft.Owin.Security.AuthenticationManager:
public async Task<AuthenticateResult> AuthenticateAsync(string authenticationType)
{
IEnumerable<AuthenticateResult> source = await this.AuthenticateAsync(new string[] { authenticationType });
return source.SingleOrDefault<AuthenticateResult>(); //<== this line throws because there are two
}
(authenticationType was "ExternalCookie" in my case)
source to share
see WebApi OAuth UseOAuthBearerAuthentication gives "The sequence contains more than one element" error . I fixed it myself by commenting
app.UseOAuthAuthorizationServer(OAuthOptions);
instead, but I'm guessing they are not compatible with both at the same time?
source to share