public ActionResult ResetPasswordForUser(string email) { var emailExists = new UsersModels().getUserByEmail(email); if (emailExists == null) { return(Json(new { status = false })); } var newPassword = utils.RandomChar(8); var updatedUser = new UsersModels().updatePassword(emailExists, newPassword); string body = new ResetPasswordForUser().body(updatedUser.display_name, newPassword); var isTrue = utils.SendEmail(emailExists.email, "Đặt lại mật khẩu", body, "", ""); if (isTrue) { return(Json(new { status = true })); } return(Json(new { status = false })); }
public async Task <IActionResult> UserSettings(ResetPasswordForUser PasswordReset) { if (!ModelState.IsValid) { ViewBag.error = "Invalid parameters"; return(View()); } // Get the variables var CurrentPassword = PasswordReset.CurrentPassword; var NewPassword = PasswordReset.NewPassword; var ConfirmNewPassword = PasswordReset.ConfirmNewPassword; { // Get the user from his current claim and verify it against the database var userId = User.Claims.FirstOrDefault(c => c.Type == "Id")?.Value; if (userId != null) { // Check if the password matches in the database var user = _context.User.FirstOrDefault(u => u.Id.ToString() == userId && u.Password == CurrentPassword); if (user != null) { user.Password = NewPassword; await _context.SaveChangesAsync(); ViewBag.success = "Password was successfully changed!"; } else { ViewBag.error = "Incorrect Password"; } } else { ViewBag.error = "No Session"; } } return(View()); }