userManager.CreateAsync System.ObjectDisposedException未處理

asp.net-core asp.net-core-mvc asp.net-identity-3 entity-framework-core

我們正在嘗試在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.

我已經重新啟動了我的機器以防它只是“其中一件事”,但錯誤再次發生。

一般承認的答案

使用async void方法很少是一個好主意,並且是引入奇怪競爭條件的最佳方式,例如您正在經歷的條件:由於您的CreateUser方法不返回任務,因此ExternalLoginCallback和請求無法等待它在CreateUser有時間執行數據庫操作之前完成(當請求完成時,DI系統調用Dispose on scoped dependencies,如EF上下文)。

更新您的CreateUser方法以返回一個Task並從ExternalLoginCallback等待它,它應該工作:

await CreateUser(info);


Related

許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
這個KB合法嗎? 是的,了解原因
許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
這個KB合法嗎? 是的,了解原因