2014-11-24 87 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()); 
相關問題