简体   繁体   中英

Adding External Authentication in ASP.NET MVC

I am trying to add external authentication using Google to an MVC webapp

I get all required information from Google, and after saving my appUser to DB, I am doing this:

var info = await AuthenticationManager.GetExternalLoginInfoAsync();
if (info == null)
{
    return View("ExternalLoginFailure");
}

// Password is a required field, although not actually needed here
model.Password = Guid.NewGuid().ToString();

AppUser user = await _profileService.CreateUserProfile(model, UserManager, true);

if (user != null)
{
    var result = await UserManager.AddLoginAsync(user.Id, info.Login);
    if (result.Succeeded)
    {
        await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);
        return RedirectToLocal(returnUrl);
    }
    AddErrors(result);
}

And in SignInManager.SignInAsync I am getting an exception

Sequence Contains More than 1 element

Stack trace is not very informative:

--- 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 Microsoft.AspNet.Identity.TaskExtensions.CultureAwaiter`1.GetResult()
   at Microsoft.AspNet.Identity.Owin.SignInManager`2.<ExternalSignInAsync>d__1d.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 AccountController.<ExternalLoginCallback>d__31.MoveNext() 

I am a bit stuck here, no idea how to get more information and where to look.

Did you edited your DB manually?

This exception usually indicates that you searched something on the DB thinking you have only 1 row returned, and when you trying to access it, you have 2 rows (or more) so id doesn't know which row to access.

Using the Identity, it makes sure those scenarios will not accord, this is why I've asked if you did something to your DB manually.

Shaul

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM