4

我们正在尝试在 ASP.Net 5 项目中使用 Microsoft 帐户身份验证。我们不需要本地身份验证,也不需要用户名。

在 Web 应用程序的 ASP.Net 5 模板中,使用外部提供程序登录后,控制权返回到ExternalLoginCallbackAccountController。

如果用户未在本地注册,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);
        }
    }

ExternalLoginConfirmationCreateUser 实现从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.

我已经重新启动了我的机器,以防万一它只是“其中之一”,但错误再次出现。

4

1 回答 1

11

使用async void方法很少是一个好主意,并且是引入奇怪的竞争条件的最好方法,比如你正在经历的那个:因为你的CreateUser方法不返回任务,它不能被等待,ExternalLoginCallback并且请求完成之前CreateUser有执行数据库操作的时间(当请求完成时,DI 系统调用Dispose范围依赖项,如您的 EF 上下文)。

更新您的CreateUser方法以返回 aTask并等待它ExternalLoginCallback,它应该可以工作:

await CreateUser(info);
于 2015-12-31T01:56:20.627 回答