首页 文章

ASP.NET Identity UserManager.CreateAsync()最近是否更新了重大更改?

提问于
浏览
0

几个月前,我创建了自己的ASP.NET身份实现,重写了UserStore以使用dapper和自定义sql连接而不是Entity Framework . 它当时运作良好 .

现在我更新了所有的nuget包,从那时起我一直在努力解决问题 . 主要是当我通过调用 var result = await UserManager.CreateAsync(user, newAccount.Password); 注册一个新用户时它创建用户并执行所有其他检查就好了,但然后抛出一个奇怪的错误说 Invalid operation. The connection is closed.

它好像UserManager.CreateAsync有一个需要重写的新方法,但我完全不知道它可能是什么 .

作为参考,以下是我实现的部分内容:

Account Controller:

[Authorize]
            public class AccountController : Controller
            {

                public UserManager<User> UserManager { get; private set; }
                public UserTokenProvider UserTokenProvider { get; set; }

                public AccountController() : this(new UserManager<User>(new UserStore(ConfigurationManager.ConnectionStrings["DBConn"].ConnectionString)))
                {
                }

                public AccountController(UserManager<User> userManager)
                {
                    UserManager = userManager;
                    UserManager.PasswordHasher = new NoPasswordHasher();

                }

...

        [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Register(RegistrationModel newAccount)
        {
            try
            {
                if (DbConfig.MaintenanceMode) return RedirectToAction("ComingSoon", "Home");
                if (ModelState.IsValid)
                {
                    var user = new User(newAccount);

                    var result = await UserManager.CreateAsync(user, newAccount.Password);
                    if (result.Succeeded)
                    {
                        await SignInAsync(user, isPersistent: false);
                        var userIn = await UserManager.FindByEmailAsync(newAccount.UserName);
                        if (!userIn.EmailConfirmed)
                        {
                            await SendValidationEmail(userIn);
                            return RedirectToAction("ConfirmationSent", new {userName = user.UserName});
                        }
                        return RedirectToAction("Index", "Home");
                    }
                    else
                    {
                        AddErrors(result);
                    }
                }

                // If we got this far, something failed, redisplay form
                return View(newAccount);
            }
            catch (Exception ex)
            {
                var msg = ex.Message;

                return View(newAccount);
            }
        }

User Store:

public class UserStore : IUserStore<User>, IUserLoginStore<User>, IUserPasswordStore<User>, IUserSecurityStampStore<User>, IUserRoleStore<User>, IUserEmailStore<User>
        {
            private readonly string _dbConn;


            public UserStore(string conn = null)
            {
                if (conn != null)
                    _dbConn = conn;
                else
                    _dbConn = DbConfig.ConnectionString;
            }

            public void Dispose()
            {
            }



            public virtual Task CreateAsync(User user)
            {
                using (var _conn = new SqlConnection(_dbConn))
                {
                    if (_conn.State == ConnectionState.Closed) _conn.Open();
                    return _conn.ExecuteAsync("users_UserCreate",
                        new
                        {
                            @UserId = user.Id,
                            @UserName = user.UserName,
                            @PasswordHash = user.PasswordHash,
                            @SecurityStamp = user.SecurityStamp
                        }, commandType: CommandType.StoredProcedure);

                }
            }

... Remaining methods omitted for brevity ...

你会注意到UserStore.CreateAsync()函数有 if (_conn.State == ConnectionState.Closed) _conn.Open(); ,因为这是多个线程关于连接关闭错误的建议 . 即使没有这一行,查询也能正常工作并正确地将新用户插入数据库 .

在UserManager.CreateAsync()调用UserStore.CreateAsync()之后,错误来自某处 .

知道缺少什么吗?

1 回答

  • 0

    答案是否定的,ASP.NET身份并没有随着重大变化而改变 .

    使用DotNetPeek我查看了Identity库,看看在UserManager.CreateAsync()期间调用了哪些方法,它只调用UserStore.CreateSync和密码更新 .

    在玩完代码之后,我突然意识到在等待UserManager.CreateSync时,对UserStore.CreateSync的内部调用不是 . 坦克必须重写 public virtual Task CreateAsync(User user) 并且不得不返回一个不等待的任务,我们必须处理一些代码以等待Dapper的响应,然后再将其作为任务的一部分返回 .

    所以,这是更新的UserStore.CreateAsync覆盖可行 . 注意:实际上并不需要 if (_conn.State == ConnectionState.Closed) _conn.Open(); ,因为在方法完成之前连接正在关闭,Dapper在为您处理连接方面做得非常出色 .

    public virtual Task CreateAsync(User user)
    {
        using (var _conn = new SqlConnection(_dbConn))
        {
            var result = _conn.ExecuteAsync("users_UserCreate", new
                        {
                            @UserId = user.Id,
                            @UserName = user.UserName,
                            @PasswordHash = user.PasswordHash,
                            @SecurityStamp = user.SecurityStamp
                        }, commandType: CommandType.StoredProcedure).ConfigureAwait(true);
    
            return Task.FromResult(result);
        }
    }
    

    希望这将有助于未来的其他人面临同样的问题 .

相关问题