public async Task <ActionResult> ChangePassword(ChangePassViewModel model) { if (!ModelState.IsValid) { return(View(model)); } var id = Guid.Parse(Session["UserId"].ToString()); var user = await _db.Users.FindAsync(id); if (user == null) { return(HttpNotFound()); } if (!EncryptDecrypt.CompareTwoMd5String(model.CurrentPassword, user.Password)) { ModelState.AddModelError("", "Mật khẩu hiện tại không đúng"); return(View(model)); } user.Password = EncryptDecrypt.GetMd5(model.NewPassword); _db.Entry(user).State = EntityState.Modified; await _db.SaveChangesAsync(); ViewData["message"] = "Thay đổi mật khẩu thành công!"; return(View()); }
public async Task <ActionResult> Login(LoginViewModel model, string returnurl) { try { var decodeUrl = string.Empty; if (!ModelState.IsValid) { return(View(model)); } var user = await _db.Users.SingleOrDefaultAsync(x => x.Email.Equals(model.Email)); if (user == null) { ModelState.AddModelError("", "Tài khoản không tồn tại"); return(View(model)); } if (!EncryptDecrypt.CompareTwoMd5String(model.Password, user.Password)) { ModelState.AddModelError("", "Mật khẩu không chính xác"); return(View(model)); } if (!user.Activated) { ModelState.AddModelError("", "Tài khoản đang tạm khóa"); return(View(model)); } Session["UserId"] = user.Id; if (!string.IsNullOrEmpty(returnurl)) { if (!string.IsNullOrEmpty(returnurl)) { decodeUrl = Server.UrlDecode(returnurl); return(Redirect(decodeUrl)); } } return(RedirectToAction("index", "home")); } catch (Exception ex) { ModelState.AddModelError("", ex.Message); return(View(model)); } }