handles duplicate email at register

This commit is contained in:
Paul Schneider 2026-09-06 19:47:38 +01:00
commit 703757d326

View file

@ -467,8 +467,25 @@ IHtmlLocalizerFactory htmlLocalizerFactory,
if (ModelState.IsValid)
{
var existingUser = await _userManager.FindByEmailAsync(model.Email);
if (existingUser is not null)
{
ModelState.AddModelError(nameof(model.Email), _localizer["DuplicateEmail"]);
return View(model);
}
var user = new ApplicationUser { UserName = model.UserName, Email = model.Email };
var result = await _userManager.CreateAsync(user, model.Password);
IdentityResult result;
try
{
result = await _userManager.CreateAsync(user, model.Password);
}
catch (DbUpdateException ex) when (IsDuplicateEmailViolation(ex))
{
_logger.LogWarning(ex, "Registration rejected: duplicate email '{Email}'.", model.Email);
ModelState.AddModelError(nameof(model.Email), _localizer["DuplicateEmail"]);
return View(model);
}
if (result.Succeeded)
{
_logger.LogInformation(3, "User created a new account with password.");
@ -517,6 +534,21 @@ IHtmlLocalizerFactory htmlLocalizerFactory,
return View(model);
}
private static bool IsDuplicateEmailViolation(Exception exception)
{
for (var current = exception; current is not null; current = current.InnerException)
{
if (current is PostgresException pg
&& pg.SqlState == PostgresErrorCodes.UniqueViolation
&& string.Equals(pg.ConstraintName, "AK_AspNetUsers_Email", StringComparison.Ordinal))
{
return true;
}
}
return false;
}
[Authorize, HttpPost, ValidateAntiForgeryToken]
public async Task<IActionResult> SendConfirationEmail()
{