public ActionResult ForgotPassWord(ForgotPasswordModel model) { if (!ModelState.IsValid) { var strError = ""; //var lstError = new List<ErrorViewModel>(); var Keys = ModelState.Keys.ToList(); var Values = ModelState.Values.ToList(); //Collect all errors for (int i = 0; i < Keys.Count; i++) { if (Values[i].Errors != null && Values[i].Errors.Count > 0) { strError += Values[i].Errors.FirstOrDefault().ErrorMessage; break; } } return(Json(strError)); } else { var newPassWord = StringHelper.GenerateRandomString(6, new Random()); //Update User var user = _userRepository.GetUserByUsername(model.Email); if (user != null) { using (UnitOfWork) { string passwordSalt = PasswordHelper.CreatePasswordSalt(PasswordHelper.DEFAULT_SALT_SIZE); string hashedPassword = PasswordHelper.CreatePasswordHash(newPassWord, passwordSalt); user.Password = hashedPassword; user.PasswordSalt = passwordSalt; user.NeedChangePassWord = true; user.NeedRemindChangePassword = true; _userRepository.Update(user); } } else { return(Json("This user not existed in system.")); } //Send Mail // 0: New password // 1: Url login // 2: Url image sign in // 3: Url host string mess = ""; string dataPath = System.Web.HttpContext.Current.Server.MapPath("~/Content/EmailTemplates"); IniFile ini = new IniFile(dataPath + "\\SendMailForgotPassWord.ini"); var title = ini.GetStringValue("Title"); var subject = ini.GetStringValue("Subject"); var content = ini.GetStringValue("Content"); object[] obj = new object[5]; obj[0] = newPassWord; obj[1] = WebHelpers.GetUrlHost(); obj[2] = WebHelpers.GetUrlHost() + "/Content/images/SignIn.png"; obj[3] = WebHelpers.GetUrlHost(); _mailServerService.SendByServer(model.Email, subject, string.Format(content, obj), null, ref mess); } return(Json("true")); }