password reset added

This commit is contained in:
2026-06-28 08:22:40 -04:00
parent 53f7a9f8f1
commit ba1ef080ad
4 changed files with 120 additions and 1 deletions

View File

@@ -202,6 +202,51 @@ public class UserManagementController : Controller
return RedirectToAction(nameof(Index));
}
[HttpGet]
public async Task<IActionResult> ResetPassword(string id)
{
var user = await _userManager.FindByIdAsync(id);
if (user == null) return NotFound();
return View(new ResetPasswordDto
{
Id = user.Id,
Email = user.Email ?? string.Empty,
FullName = user.FullName
});
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> ResetPassword(ResetPasswordDto dto)
{
var user = await _userManager.FindByIdAsync(dto.Id);
if (user == null) return NotFound();
// Keep display fields populated regardless of the posted values.
dto.Email = user.Email ?? string.Empty;
dto.FullName = user.FullName;
if (!ModelState.IsValid)
{
return View(dto);
}
var token = await _userManager.GeneratePasswordResetTokenAsync(user);
var result = await _userManager.ResetPasswordAsync(user, token, dto.NewPassword);
if (!result.Succeeded)
{
foreach (var error in result.Errors)
{
ModelState.AddModelError(string.Empty, error.Description);
}
return View(dto);
}
TempData["SuccessMessage"] = $"Password reset successfully for {user.Email}.";
return RedirectToAction(nameof(Index));
}
private async Task PopulateDropdowns()
{
ViewBag.Roles = await _roleManager.Roles.Select(r => r.Name).ToListAsync();