2014-11-24 88 views
1

在尝试使用谷歌在外部身份验证,应用程序给了我以下异常:序列包含多个元素Microsoft.Owin.Security.AuthenticationManager

<错误> <消息>发生了错误。 <ExceptionMessage>序列包含一个以上的元件</ExceptionMessage > <ExceptionType> System.InvalidOperationException </ExceptionType > <堆栈跟踪> 在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()在System.Web.Http.HostAuthenticationFilter.d__0.MoveNext()---从以前位置抛出异常的堆栈跟踪结束---在System.Runtime.CompilerServices的System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务) System.Runtime.CompilerService上的.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务) s.TaskAwaiter.GetResult()在System.Web.Http.Controllers.AuthenticationFilterResult.d__0.MoveNext()---从以前的位置抛出异常的堆栈跟踪结束---在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()

我已经配置我的网络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); 
} 
+0

我发现解决方案[这里](http://stackoverflow.com/questions/24978940/webapi-oauth-useoauthbearerauthentication-gives-sequence-contains-more-than-one)它会帮助你 – 2016-02-17 04:39:21

回答

1

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

2

我也拿到了这个错误,它原来我有以下几点:

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

但也在控制器上:

[HostAuthentication(DefaultAuthenticationTypes.ExternalCookie)] 

(为了完整性起见:我用web.api仅owin,使用IIS owin主机)

解决方案:卸下的这些固定的问题之一,所以我想加入这样的控制器属性,是相同的配置两次。寻找这样的可疑的东西(他们也可能会在您使用的库配置!)

关于发生错误的某些其它背景资料:

在我的情况下,观察到大量的时间(但并非总是如此!)发生错误。调试了解到这个有问题的方法存在于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是在我的情况 “ExternalCookie”)

0

我解决了这个使用以下两个配置设置在一起(我使用刷新标记):

app.UseOAuthAuthorizationServer(options); 
app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions()); 
相关问题