public async Task <IActionResult> ValidatePost(FValidateToken validateToken) { IUser user = this._context.Users.SingleOrDefault(u => u.Cpf == validateToken.Cpf); if (user == null) { this._appManager.addAlert("danger", "O CPF inserido não existe.", this.HttpContext); return(RedirectToAction("Validate", "Account", new { validateToken.Token })); } IUserRecover recover = this._context.UsersRecover.SingleOrDefault(r => r.Token == validateToken.Token && r.UserId == user.Id); if (recover == null) { this._appManager.addAlert("danger", "O Token inserido está incorreto.", this.HttpContext); return(RedirectToAction("Validate", "Account", new { validateToken.Token })); } if (recover.Validated) { this._appManager.addAlert("warning", "O Token inserido já foi validado.", this.HttpContext); return(RedirectToAction("Validate", "Account", new { validateToken.Token })); } if (new Cripto(validateToken.NovaSenha).Encrypted == user.Senha) { this._appManager.addAlert("danger", "Você não pode utilizar uma senha igual a antiga.", this.HttpContext); return(RedirectToAction("Validate", "Account", new { validateToken.Token })); } if (this._appManager.Timestamp - recover.Created > 86400) // 86400 = 1 dia { this._appManager.addAlert("danger", "Este Token já expirou faz mais de 1 dia. Envie um e-mail novamente para recupearar sua senha.", this.HttpContext); return(RedirectToAction("Validate", "Account", new { validateToken.Token })); } user.Senha = new Cripto(validateToken.NovaSenha).Encrypted; recover.Validated = true; this._context.UsersRecover.Update(recover); this._context.Users.Update(user); this._appManager.addAlert("success", "Sua senha foi alterada com sucesso. ", this.HttpContext); await this._context.SaveChangesAsync(); return(RedirectToAction("Index", "Home")); }
/// <summary> /// Envia um E-mail para o Usuário com instruções para recuperar a senha /// </summary> /// <param name="user">IUser</param> /// <param name="context">IDatabaseContext</param> /// <returns>async Task</returns> public async Task sendRecoveryEmail(IUser user, IDatabaseContext context) { IUserRecover recover = new IUserRecover { UserId = user.Id, Token = generateToken(8), Validated = false, Created = Timestamp }; context.UsersRecover.Add(recover); await context.SaveChangesAsync(); new Thread(async() => { MailMessage message = new MailMessage(); message.From = new MailAddress("*****@*****.**"); message.To.Add(user.Email); message.IsBodyHtml = true; message.Body = this.getEmailBody().Replace("%nome%", user.Nome).Replace("%token%", recover.Token); message.Subject = "Recuperação de Conta - Projeto Saúde"; await this.smtpClient.SendMailAsync(message); }).Start(); }