public string ChangePassword(ChangePassword_ViewModel mode) { try { LaborerandAdmin emps = db.LaborerandAdmins.Where(c => c.IDLaborer == mode.ID).Single <LaborerandAdmin>(); if (emps.password == MaHoaMD5.EncodePasswordMd5(mode.oldPassword)) { emps.password = MaHoaMD5.EncodePasswordMd5(mode.newPassword); db.Entry(emps).State = System.Data.Entity.EntityState.Modified; db.SaveChanges(); return("Thanh cong!"); } else { return("Mat khau sai!"); } } catch (Exception e) { Console.WriteLine(e); return(e.ToString()); } }
public ActionResult ChangePassword(ChangePassword_ViewModel models) { if (ModelState.IsValid) { //work around, in order to change password, need to know the old password. //since not using old password field, need membership provider to reset //the password then use the generated password as the old password //Generates a password token for this specific username in the session //Requires a username to generate the token for string GeneratedPasswordToken = WebSecurity.GeneratePasswordResetToken(User.Identity.Name); string username = User.Identity.Name; //Resets the password to the new password, usually the token is sent via email to the user bool status = WebSecurity.ResetPassword(GeneratedPasswordToken, models.ConfirmPassword); if (status) { System.Diagnostics.Trace.WriteLine("Password CHange worked"); ViewBag.Message = "Password Change SUCCESSFULLY"; } else { System.Diagnostics.Trace.WriteLine("Password change didnt work" + User.Identity.Name); ViewBag.Message = "Password Change was UNSUCCESSFUL"; } /* * var query = (from x in db.Logins * where x.CustomerID.Equals(100) * select x).Single(); * * //query.Password = models.ConfirmPassword; * db.SaveChanges(); */ } return(View()); }