public ActionResult ForgotPassword(string EmailID) { string resetCode = Guid.NewGuid().ToString(); var verifyUrl = "/Account/ResetPassword/" + resetCode; var link = Request.Url.AbsoluteUri.Replace(Request.Url.PathAndQuery, verifyUrl); using (var context = new WebBaseSystemEntities()) { var getUser = (from s in context.Users where s.EmailAddress == EmailID select s).FirstOrDefault(); if (getUser != null) { getUser.ResetPasswordCode = resetCode; //This line I have added here to avoid confirm password not match issue , as we had added a confirm password property context.Configuration.ValidateOnSaveEnabled = false; context.SaveChanges(); var subject = "Password Reset Request"; var body = "Hi " + getUser.FirstName + ", <br/> You recently requested to reset your password for your account. Click the link below to reset it. " + " <br/><br/><a href='" + link + "'>" + link + "</a> <br/><br/>" + "If you did not request a password reset, please ignore this email or reply to let us know.<br/><br/> Thank you"; SendEmail(getUser.EmailAddress, body, subject); ViewBag.Message = "Reset password link has been sent to your email id."; } else { ViewBag.Message = "User doesn't exists."; return(View()); } } return(View()); }
public ActionResult ResetPassword(ResetPassword model) { var message = ""; if (ModelState.IsValid) { using (var context = new WebBaseSystemEntities()) { var user = context.Users.Where(a => a.ResetPasswordCode == model.ResetCode).FirstOrDefault(); if (user != null) { //you can encrypt password here, we are not doing it user.Pass = model.NewPassword; //make resetpasswordcode empty string now user.ResetPasswordCode = ""; //to avoid validation issues, disable it context.Configuration.ValidateOnSaveEnabled = false; context.SaveChanges(); message = "New password updated successfully"; } } } else { message = "Something invalid"; } ViewBag.Message = message; return(View(model)); }