public Task <IActionResult> SendConfirmEmail(AskResetPasswordInput input) { return(Task.Run <IActionResult>(async() => { var user = _db.Single <User>(c => c.Username == input.Username); if (user == null) { return new StatusCodeResult((int)HttpStatusCode.BadRequest); } var confirmEmail = _db.Single <ConfirmEmail>(r => r.UserId == user.Id && !r.Used); if (confirmEmail == null) { return BadRequest(); } confirmEmail.Uuid = Guid.NewGuid().ToString(); _db.Update <ResetPassword>(confirmEmail.Id, new Dictionary <string, object> { { "Uuid", confirmEmail.Uuid } }); var mailInput = new SendMailInput { From = "PolyHx <*****@*****.**>", To = new[] { user.Username }, Subject = "Confirmer votre compte | Confirm your account", Template = "confirm_email", Html = "<html></html>", Text = "Text", Variables = new Dictionary <string, string> { { "url", $"{Environment.GetEnvironmentVariable("CONFIRM_EMAIL_URL")}/{confirmEmail.Uuid}" } } }; await _mailService.SendEmail(mailInput); return Ok(new {}); })); }
public IActionResult Create(AskResetPasswordInput input) { Task.Run(async() => { Console.WriteLine("Sending the reset password email to the user"); if (!ModelState.IsValid) { Console.WriteLine("The model isn't valid"); return; } var user = _db.Single <User>(c => c.Username.ToLower() == input.Username.ToLower()); if (user == null) { Console.WriteLine("No user found with this username {0}", input.Username); return; } ResetPassword resetPassword; try { resetPassword = _db.Single <ResetPassword>(c => c.UserId == user.Id && !c.Used); } catch (Exception) { Console.WriteLine("An error occured while getting the reset password."); resetPassword = null; } if (resetPassword == null) { resetPassword = new ResetPassword { UserId = user.Id, Uuid = Guid.NewGuid().ToString() }; _db.Add(resetPassword); } else { resetPassword.Uuid = Guid.NewGuid().ToString(); _db.Update <ResetPassword>(resetPassword.Id, new Dictionary <string, object> { { "Uuid", resetPassword.Uuid } }); } var mailInput = new SendMailInput { From = "CS Games <*****@*****.**>", To = new[] { user.Username }, Subject = "Réinitialisation du Mot de Passe | Password Reset", Template = "password_reset", Html = "<html></html>", Text = "Text", Variables = new Dictionary <string, string> { { "url", $"{Environment.GetEnvironmentVariable("RESET_PASSWORD_URL")}/{resetPassword.Uuid}" } } }; var res = await _mailService.SendEmail(mailInput); Console.WriteLine("The email was sent successfully"); }); return(Ok()); }