我们正在尝试在 ASP.Net 5 项目中使用 Microsoft 帐户身份验证。我们不需要本地身份验证,也不需要用户名。
在 Web 应用程序的 ASP.Net 5 模板中,使用外部提供程序登录后,控制权返回到ExternalLoginCallback
AccountController。
如果用户未在本地注册,ExternalLoginCallback
则将用户返回到注册屏幕。我试图修改ExternalLoginCallback
为自动注册新用户,如下所示。
public async Task<IActionResult> ExternalLoginCallback(string returnUrl = null)
{
var info = await _signInManager.GetExternalLoginInfoAsync();
if (info == null)
{
return RedirectToAction(nameof(Login));
}
// Sign in the user with this external login provider if the user already has a login.
var result = await _signInManager.ExternalLoginSignInAsync(info.LoginProvider, info.ProviderKey, isPersistent: false);
if (result.Succeeded)
{
_logger.LogInformation(5, "User logged in with {Name} provider.", info.LoginProvider);
return RedirectToLocal(returnUrl);
}
if (result.RequiresTwoFactor)
{
return RedirectToAction(nameof(SendCode), new { ReturnUrl = returnUrl });
}
if (result.IsLockedOut)
{
return View("Lockout");
}
else
{
// If the user does not have an account, then ask the user to create an account.
//ViewData["ReturnUrl"] = returnUrl;
//ViewData["LoginProvider"] = info.LoginProvider;
//var email = info.ExternalPrincipal.FindFirstValue(ClaimTypes.Email);
//return View("ExternalLoginConfirmation", new ExternalLoginConfirmationViewModel { Email = email });
// The user has not previously logged in with an external provider. Create a new user.
CreateUser(info);
return RedirectToLocal(returnUrl);
}
}
ExternalLoginConfirmation
CreateUser 实现从Web 应用程序的 ASP.Net 5 模板中复制的代码。
private async void CreateUser(ExternalLoginInfo info)
{
var email = info.ExternalPrincipal.FindFirstValue(ClaimTypes.Email);
var user = new ApplicationUser { UserName = email, Email = email };
var result = await _userManager.CreateAsync(user);
if (result.Succeeded)
{
result = await _userManager.AddLoginAsync(user, info);
if (result.Succeeded)
{
await _signInManager.SignInAsync(user, isPersistent: false);
_logger.LogInformation(6, "User created an account using {Name} provider.", info.LoginProvider);
}
}
AddErrors(result);
}
上线抛出错误
var result = await _userManager.CreateAsync(user);
错误是
System.ObjectDisposedException was unhandled Message: An unhandled exception of type 'System.ObjectDisposedException' occurred in mscorlib.dll. Additional information: Cannot access a disposed object.
我已经重新启动了我的机器,以防万一它只是“其中之一”,但错误再次出现。