I'm trying to implement my password recovery to the user who entered his email.
My problem is: I don't know how it should work exactly, I can send email successfully, but the link sent to him need to be a link to the recovery page with his token at the url I assume?
I need to know how I involve the url with my Angular site...
In my code I'm sending a link to the user with the link but I think its need to be after the user get to the page with the password change.
Here's my code:
// Post Request
// POST : /api/AppUser/Login
#region Recover Password
[HttpPost]
[Route("RecoverPassword")]
public async Task<IActionResult> RecoverPassword(RecoverPasswordModel model)
{
var user = await _userService.FindByEmail(model.Email);
string code = await _userService.GetRecoveryToken(user);
string recoveryLink = Url.Action("RecoveryLink", "AppUser", new
{
UserId = user.Id,
Code = code
}, protocol: HttpContext.Request.Scheme);
bool finalRes = _userService.SendEmail(user, confirmationTokenLink);
if (finalRes)
return Ok();
else
return BadRequest(new { message = "No such user with this email" });
}
[HttpGet("[action]")]
[AllowAnonymous]
public async Task<IActionResult> RecoveryLink(string userId, string code, string newPassword)
{
if (string.IsNullOrEmpty(userId) || string.IsNullOrEmpty(code))
return BadRequest(new { message = "Code or ID in empty" });
var user = await _userService.FindByID(userId);
if (user == null)
return BadRequest(new { message = "User not found!" });
var result = await _userService.ChangePassword(user, code, newPassword);
if (result.Succeeded)
return Content($"<html><body><h1>Your Password changed successfully!!! </h1><hr><a href= { EMAIL_VERIFIED_URI }> Please Login now! </a></body></html>", "text/html");
else
return BadRequest(new { message = "Reset failed!" });
}