public ActionResult SendCodeByEmail(EmailResetPassword emailResetPassword) { var UserInDb = db.Users.SingleOrDefault(r => r.UserEmail == emailResetPassword.Email_To_Recover); if (UserInDb != null) { var RanNum = GenerateRandomNoForUser(); TapleReseetpassword tapleReseetpassword = new TapleReseetpassword(); tapleReseetpassword.UserId = UserInDb.Id; tapleReseetpassword.Code_send = RanNum.ToString(); db.TapleReseetpassword.Add(tapleReseetpassword); db.SaveChanges(); //send email MailMessage mailMessage = new MailMessage("*****@*****.**", emailResetPassword.Email_To_Recover); mailMessage.Subject = "رمز تغير كلمة مرور "; mailMessage.Body = RanNum + ": رمز التفعيل"; mailMessage.IsBodyHtml = false; SmtpClient client = new SmtpClient(); client.Host = "smtp.gmail.com"; client.Port = 587; client.EnableSsl = true; NetworkCredential nc = new NetworkCredential("*****@*****.**", "Password"); client.UseDefaultCredentials = true; client.Credentials = nc; client.Send(mailMessage); return(View()); } else { ViewBag.Message = "invalid"; } return(View()); }
public async Task ResetPasswordByEmail(ResetPasswordByEmailRequest request) { var user = _userService.GetUserByEmail(request.Email); if (user == null) { throw new ExceptionWithHttpStatus(System.Net.HttpStatusCode.BadRequest, "Email não encontrado!"); } user = _userService.ResetPassword(user.UsuarioId); EmailResetPassword.Enviar(user.Email, user.Name, user.Password); }
public async Task ResetPasswordByEmail([FromBody] EmailResetPassword model) { if (model == null) { throw new CustomException(Errors.REQUEST_NOT_NULL, Errors.REQUEST_NOT_NULL_MSG); } else if (string.IsNullOrEmpty(model.Email)) { throw new CustomException(Errors.EMAIL_NOT_NULL, Errors.EMAIL_NOT_NULL_MSG); } else if (string.IsNullOrEmpty(model.Token)) { throw new CustomException(Errors.TOKEN_NOT_NULL, Errors.TOKEN_NOT_NULL_MSG); } else if (string.IsNullOrEmpty(model.NewPassword)) { throw new CustomException(Errors.PASSWORD_NOT_NULL, Errors.PASSWORD_NOT_NULL_MSG); } await _accountService.ResetPasswordByEmail(model); }
public async Task ResetPasswordByEmail(EmailResetPassword model) { var account = await CheckExsitByUserNameAsync(model.Email); if (account == null) { throw new CustomException(Errors.ACCOUNT_NOT_FOUND, Errors.ACCOUNT_NOT_FOUND_MSG); } var now = DateTime.Now; var request = await _context.RequestResetPasswords.FirstOrDefaultAsync(s => s.Email == model.Email && s.Token == model.Token && s.ExpiredAt > now); if (request == null) { throw new CustomException(Errors.TOKEN_IS_EXPIRED, Errors.TOKEN_IS_EXPIRED_MSG); } account.Password = _pwdHasher.HashPassword(account, model.NewPassword); account.SecurityStamp = GenerateSecurityStamp(); account.ModifiedAt = DateTime.Now; await _context.SaveChangesAsync(); }
public async Task ResetPassword(string usuarioId) { var user = _userService.ResetPassword(new Guid(usuarioId)); EmailResetPassword.Enviar(user.Email, user.Name, user.Password); }