A few months ago I created my own implementation of ASP.NET Identity, overriding the UserStore to use dapper and a custom sql connection instead of Entity Framework. It worked fine at the time.
Now I updated all of the nuget packages today and I've been fighting problems since. Primarily when I register a new user by calling var result = await UserManager.CreateAsync(user, newAccount.Password); It creates the user and performs all other checks just fine, but then throws a bizarre error saying Invalid operation. The connection is closed.
Its as though UserManager.CreateAsync has a new method that needs to be overridden, but I have absolutely no idea what it could be.
For reference, here are parts of my implementation:
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 ...
You'll notice that the UserStore.CreateAsync() function has if (_conn.State == ConnectionState.Closed) _conn.Open(); as this was the suggestion from multiple threads regarding the connection closed error. Even without this line the query works fine and inserts a new user into the database correctly.
The error is coming from somewhere after UserStore.CreateAsync() is called by UserManager.CreateAsync().
Any idea what's missing?