Sequence包含多个元素Microsoft.Owin.Security.AuthenticationManager

时间:2014-11-24 09:21:48

标签: c# oauth-2.0 asp.net-web-api2 owin google-authentication

在尝试使用Google进行外部身份验证时,应用程序会给我以下例外情况:

  

&LT;错误&GT;   &lt; Message&gt;发生错误。   &lt; ExceptionMessage&gt;序列包含多个元素&lt; / ExceptionMessage&gt;   &LT; ExceptionType&GT; System.InvalidOperationException&LT; / ExceptionType&GT;   &LT;堆栈跟踪&GT;       at 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()---来自先前位置的堆栈跟踪结束,其中异常是抛出---在System.Web.Http上System.Runtime.CompilerServices.TaskAwaiter.GetResult()的System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)上的System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务) .Controllers.AuthenticationFilterResult.d__0.MoveNext()---在抛出异常的前一个位置的堆栈跟踪结束---在System.Runtime.CompilerServices.TaskAwaiter的System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)处。 System.Web.Http.Dispatcher.HttpControllerDispatcher.d__1.MoveNext()上的System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()处理HandleNonSuccessAndDebuggerNotification(任务任务)              

我已将Web Api oAuth配置如下:

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);
}

4 个答案:

答案 0 :(得分:6)

请检查,您可能正在使用 app.UseOAuthBearerTokens(OAuthOptions);app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions());一起

答案 1 :(得分:3)

我通过一起使用以下两个配置设置解决了这个问题(我使用刷新令牌):

app.UseOAuthAuthorizationServer(options);
app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions());

答案 2 :(得分:2)

我也遇到了这个错误,结果发现我有以下内容:

app.UseCookieAuthentication(new CookieAuthenticationOptions
        {AuthenticationType = DefaultAuthenticationTypes.ExternalCookie}

在我的SecurityConfig中 而且这个在控制器上:

[HostAuthentication(DefaultAuthenticationTypes.ExternalCookie)]

(为了完整起见:我只使用了web.api owin,使用owin IIS托管)

解决方案:删除其中一个解决了这个问题,所以我想添加这样的控制器属性与配置两次相同。寻找像这样的可疑事物(它们也可能在您使用的库中配置!)

有关错误发生位置的更多背景信息:

在我的情况下,观察到很多次(但不总是?!)错误发生。调试了解到有问题的方法存在于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为&#34; ExternalCookie&#34;在我的情况下)

答案 3 :(得分:0)

WebApi OAuth UseOAuthBearerAuthentication gives "Sequence contains more than one element" error。我自己通过评论来修复它

app.UseOAuthAuthorizationServer(OAuthOptions);

相反,但我猜他们不兼容同时拥有两个?