简体   繁体   English

在ASP.NET MVC中添加外部身份验证

[英]Adding External Authentication in ASP.NET MVC

I am trying to add external authentication using Google to an MVC webapp 我正在尝试使用Google将外部身份验证添加到MVC Web应用程序

I get all required information from Google, and after saving my appUser to DB, I am doing this: 我从Google获得了所有必需的信息,并将appUser保存到数据库后,我正在执行以下操作:

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 在SignInManager.SignInAsync中,我得到一个例外

Sequence Contains More than 1 element 序列包含1个以上的元素

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. 此异常通常表示您在数据库上搜索某些内容,认为您仅返回了1行,而当您尝试访问它时,您只有2行(或更多),因此id不知道要访问哪一行。

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 肖尔

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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