[HttpPost] [Route("ForgotPassword")] public async Task<IActionResult> ForgotPassword(ForgotPasswordViewModel model) { if (!ModelState.IsValid) { return BadRequest("Invalid data."); } var user = await _userManager.FindByEmailAsync(model.Email); if (user == null) { return NotFound("User not found."); } var token = await _userManager.GeneratePasswordResetTokenAsync(user); var encodedToken = HttpUtility.UrlEncode(token); // Send the token to the user's email var resetLink = Url.Action("ResetPassword", "Account", new { token = encodedToken, email = model.Email }, Request.Scheme); // Add email sending logic here return Ok(encodedToken); } [HttpPost] [Route("ResetPassword")] public async Task<IActionResult> ResetPassword([FromBody] ResetPasswordViewModel model) { if (!ModelState.IsValid) { return BadRequest("Invalid data."); } var user = await _userManager.FindByEmailAsync(model.Email); if (user == null) { return NotFound("User not found."); } var decodedToken = HttpUtility.UrlDecode(model.Token); var result = await _userManager.ResetPasswordAsync(user, decodedToken, model.Password); if (result.Succeeded) { return Ok("Password has been reset successfully."); } // Log errors for debugging var errors = string.Join(", ", result.Errors.Select(e => e.Description)); return BadRequest($"Error while resetting the password: {errors}"); }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter