userManager.CreateAsync System.ObjectDisposedException未处理

时间:2015-12-31 01:44:12

标签: asp.net-core asp.net-core-mvc entity-framework-core asp.net-identity-3

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

在Web应用程序的ASP.Net 5模板中,在使用外部提供程序登录后,控件将返回到AccountController中的ExternalLoginCallback

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

CreateUser实现从ExternalLoginConfirmation复制的代码,因为它出现在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.

我已经重新启动了我的机器,以防万一它只是其中之一&#39;但错误再次发生。

1 个答案:

答案 0 :(得分:9)

使用async void方法很少是一个好主意,并且是引入奇怪竞争条件的最佳方式,例如您遇到的情况:因为您的CreateUser方法无法返回任务,ExternalLoginCallback无法等待,CreateUser有时间执行数据库操作后请求完成(当请求完成时,DI系统调用Dispose在您的EF上下文等范围内的依赖关系。)

更新您的CreateUser方法以返回Task并等待ExternalLoginCallback,并且它应该有效:

await CreateUser(info);