0

なぜ外部ログインコールバックでこのエラーが発生しますか?ここで何をしているのですか?ここでMvc5 ExternalLoginCallback:「シーケンスに複数の要素が含まれている」と「AuthenticationManager.GetExternalLoginInfoAsync()が失敗しました」

輸入

using Microsoft.Owin.Security.MicrosoftAccount; 
using Microsoft.Owin.Security; 

マイAccountController.csクラス

.  
. 
. 
. 
. 
var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync(); 
if (loginInfo == null) 
{      
    return RedirectToAction("Login", new { returnUrl = returnUrl}); 
} 
. 
. 
. 
. 

マイStartup.csクラス

. 
. 
. 
app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie); 


     app.UseTwoFactorSignInCookie(DefaultAuthenticationTypes.TwoFactorCookie, TimeSpan.FromMinutes(5)); 
     // 
     app.UseExternalSignInCookie(Microsoft.AspNet.Identity.DefaultAuthenticationTypes.ExternalCookie); 
. 
. 
//https://account.live.com/developers/applications/ 
     // 
     microsoftAuthOptions = new MicrosoftAccountAuthenticationOptions() 
     { 
      ClientId = "xxxxxxx", 
      ClientSecret = "xxxxxxx", 
      CallbackPath = new PathString("/callbacks/microsoft"), 
      Provider = new MicrosoftAccountAuthenticationProvider() 
      { 
       OnAuthenticated = (context) => 
       { 
        context.Identity.AddClaim(new System.Security.Claims.Claim("MicrosoftAccountAccessToken", context.AccessToken)); 

        return Task.FromResult(0); 
       } 
      } 
     }; 
     app.UseMicrosoftAccountAuthentication(microsoftAuthOptions); 

     // 
     twitterAuthOptions = new TwitterAuthenticationOptions() 
     { 
      ConsumerKey = "xxxxxxxx", 
      ConsumerSecret = "xxxxxxx", 
      CallbackPath = new PathString("/callbacks/twitter"), 
      Provider = new TwitterAuthenticationProvider() 
      { 
       OnAuthenticated = (context) => 
       { 
        context.Identity.AddClaim(new System.Security.Claims.Claim("TwitterAccessToken", context.AccessToken)); 

        return Task.FromResult(0); 
       } 
      }, 
      BackchannelCertificateValidator = new CertificateSubjectKeyIdentifierValidator(new[] 
       { 
        "A5EF0B11CEC04103A34A659048B21CE0572D7D47", // VeriSign Class 3 Secure Server CA - G2 
        "0D445C165344C1827E1D20AB25F40163D8BE79A5", // VeriSign Class 3 Secure Server CA - G3 
        "7FD365A7C2DDECBBF03009F34339FA02AF333133", // VeriSign Class 3 Public Primary Certification Authority - G5 
        "39A55D933676616E73A761DFA16A7E59CDE66FAD", // Symantec Class 3 Secure Server CA - G4 
        "5168FF90AF0207753CCCD9656462A212B859723B", //DigiCert SHA2 High Assurance Server C‎A 
        "B13EC36903F8BF4701D498261A0802EF63642BC3" //DigiCert High Assurance EV Root CA 
       }) 
     }; 
     app.UseTwitterAuthentication(twitterAuthOptions); 

     //Configure Facebook External Login 
     facebookAuthOptions = new FacebookAuthenticationOptions() { 
      AppId = "xxxxxxxx", 
      AppSecret = "xxxxxxxx", 
      CallbackPath = new PathString("/callbacks/facebook"), 
      Provider = new FacebookAuthProvider() 
      { 
       OnAuthenticated = (context) => 
       { 
        context.Identity.AddClaim(new System.Security.Claims.Claim("FacebookAccessToken", context.AccessToken)); 

        foreach (var claim in context.User) 
        { 
         var claimType = string.Format("urn:facebook:{0}", claim.Key); 
         var claimValue = claim.Value.ToString(); 

         if (!context.Identity.HasClaim(claimType, claimValue)) 
          context.Identity.AddClaim(new System.Security.Claims.Claim(claimType, claimValue, "XmlSchemaString", "Facebook")); 
        } 

        return Task.FromResult(0); 
       } 
      } 
     }; 
     facebookAuthOptions.Scope.Add("email"); 
     facebookAuthOptions.Scope.Add("user_about_me"); 
     facebookAuthOptions.Scope.Add("user_photos"); 
     facebookAuthOptions.Scope.Add("user_location"); 
. 
. 
. 
. 

スタックトレースです:

この動作は、グーグル、TwitterやFacebookについても同様である私のコードでは、ここで View a print screen

を指す

[InvalidOperationException: Sequence contains more than one element] 
    System.Linq.Enumerable.SingleOrDefault(IEnumerable`1 source) +305 
    Microsoft.Owin.Security.<AuthenticateAsync>d__8.MoveNext() +213 
    System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +99 
    System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +58 
    Microsoft.Owin.Security.<GetExternalLoginInfoAsync>d__a.MoveNext() +189 
    System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +99 
    System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +58 
    System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() +28 
    com.hwr.Controllers.<ExternalLoginCallback>d__37.MoveNext() in C:\Users\Bourne Koloh\Documents\Visual Studio 2015\Projects\com.hwr\com.hwr.mvc5\Controllers\AccountController.cs:804 
    System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +99 
    System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +58 

。外部認証はうまくいくようですが、コールバックはこの例外をスローします。 提案があれば感謝します。

+0

スタックトレースとは何ですか? – SLaks

+0

そこにあなたはそれを持っています。私は関連する部分だけを追加しました –

答えて

1

(簡体字)私は(Microsoft.Owin.Security.AuthenticationManagerExtensionsに)GetExternalLoginInfoAsync()のソースを見つけることができませんが、逆コンパイルは明らかに:AuthenticateAsync()ため

await manager.AuthenticateAsync("ExternalCookie"); 

ソースは、あなたが見つける利用可能on CodePlex、次のとおりです。 Authenticate(authenticationTypes, AuthenticateAsyncCallback, results)呼び出して、トレース

public async Task<AuthenticateResult> AuthenticateAsync(string authenticationType) 
{ 
    return (await AuthenticateAsync(new[] { authenticationType })).SingleOrDefault(); 
} 

public async Task<IEnumerable<AuthenticateResult>> AuthenticateAsync(string[] authenticationTypes) 
{ 
    var results = new List<AuthenticateResult>(); 
    await Authenticate(authenticationTypes, AuthenticateAsyncCallback, results); 
    return results; 
} 

複雑になるが、最終的にあなたが見ているSingleOrDefault()エラーがあなたの認証の設定であるため、指定された `authenticationType"(具体的には "ExternalCookie")に対して複数のAuthenticateResultエントリが生成されています。

さらにStartup.csを投稿できる場合は、正確な問題を見つけることができます。

+0

こんにちは、あなたのお返事ありがとうございます。私は 'startup.cs'ファイルからより多くのコンテンツを追加しました。あなたが投稿する特定の部分を私に教えれば、それは良いと感じました。ファイルは少し大きいです。 –

+1

コピー/ペーストの問題ですか、本当に2つの 'UseExternalSignInCookie()'コールがありますか? – dahlbyk

+0

Oooooo!はいはい。ありがとう、私はそれに気付かなかった。重複を削除する.. –

関連する問題