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



几个月前,我创建了自己的 ASP.NET Identity实现,覆盖UserStore以使用dapper和自定义sql连接而不是实体框架。当时效果很好。

现在我今天更新了所有 nuget 包,从那以后我一直在与问题作斗争。主要是当我通过调用var result = await UserManager.CreateAsync(user, newAccount.Password);注册新用户时,它会创建用户并正常执行所有其他检查,但随后抛出一个奇怪的错误,说Invalid operation. The connection is closed.

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

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

客户控制者:

[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);
}
}

用户存储:

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()之后的某个地方。

知道缺少什么吗?

答案是否定的,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);
}
}

希望这将帮助将来面临相同问题的其他人。

最新更新