public async Task <IActionResult> ConfirmRegisterEmail([FromBody] RegisterConfirmEmail parameters) { var user = await _userManager.FindByEmailAsync(parameters.Email); if (user == null) { _logger.LogError($"AuthenticationController.ConfirmRegisterEmail email:{parameters.Email}; error:unknown user"); throw new ProblemDetailsException(400, "Error occured"); } var result = await _userManager.ConfirmEmailAsync(user, parameters.Token); if (result.Succeeded) { // https://stackoverflow.com/questions/22755700/revoke-token-generated-by-usertokenprovider-in-asp-net-identity-2-0 // we need to reset user security stamp after he confirms email, so he will get new security token // so he can't confirm register over and over again with the same token await _userManager.UpdateSecurityStampAsync(user); return(Ok()); } _logger.LogError($"AuthenticationController.ConfirmRegisterEmail email:{parameters.Email}; error:{result.Errors}"); throw new ProblemDetailsException(400, "Error occured"); }
public async Task <IActionResult> ForgotPassword(ForgotPasswordViewModel model) { if (ModelState.IsValid) { var user = await userManager.FindByEmailAsync(model.Email); if (user == null && !(await userManager.IsEmailConfirmedAsync(user))) { return(View("ForgotPasswordConfirmation", "Account")); } var code = await userManager.GeneratePasswordResetTokenAsync(user); var callbackUrl = Url.Action("ResetPassword", "Account", new { userId = user.Id, code = code, email = model.Email }, protocol: HttpContext.Request.Scheme); RegisterConfirmEmail emailService = new RegisterConfirmEmail(); await emailService.SendEmailAsync(model.Email, "Reset password", $"To reset your password, follow the <a href='{callbackUrl}'>link</a>"); return(RedirectToAction("ConfirmEmailContentView", "Account")); } return(View(model)); }
public async Task <IActionResult> Register(RegisterViewModel registerViewModel) { if (ModelState.IsValid) { User user = new User { Email = registerViewModel.Email, UserName = registerViewModel.Email, Surname = registerViewModel.Surname, Name = registerViewModel.Name, Role = "user" }; var result = await userManager.CreateAsync(user, registerViewModel.Password); if (result.Succeeded) { var code = await userManager.GenerateEmailConfirmationTokenAsync(user); var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: HttpContext.Request.Scheme); RegisterConfirmEmail email = new RegisterConfirmEmail(); await email.SendEmailAsync(registerViewModel.Email, "Confirm your account", $"Confirm registration by following the <a href='{callbackUrl}'>link</a>"); return(RedirectToAction("ConfirmEmailContentView", "Account")); } } return(View("Login", "Account")); }