public ActionResult EditUser(UserModel userModel) { try { if (userModel.UserName == null || userModel.UserName == "" || userModel.Email == null || userModel.Email == "" || userModel.Password == null || userModel.Password == "") { Session["message"] = "Please fill in all the required fields."; Session["requestStatus"] = "Error"; return(RedirectToAction("Index")); } if (userModel.Password != null && userModel.Password != "") { UnicodeEncoding uEncode = new UnicodeEncoding(); byte[] data = uEncode.GetBytes(userModel.Password); data = new System.Security.Cryptography.SHA512Managed().ComputeHash(data); userModel.Password = Convert.ToBase64String(data); } dal.EditUser(userModel); Session["message"] = "User is successfully edited"; Session["requestStatus"] = "Success"; return(RedirectToAction("Index")); } catch (Exception ex) { Session["message"] = "Unable to perform this request"; Session["requestStatus"] = "Error"; return(RedirectToAction("Index")); } }
public ActionResult ResetPassword(string email) { try { DAL.DAL dal = new DAL.DAL(); UserModel um = dal.GetUserByEmail(email); if (um == null) { Session["message"] = "Email is not associated to any registered accounts. Please enter a registered email address."; Session["requestStatus"] = "Error"; return(RedirectToAction("ResetPassword", "Login")); } Random random = new Random(); const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; string recover = new string(Enumerable.Repeat(chars, 8) .Select(s => s[random.Next(s.Length)]).ToArray()); var fromAddress = new MailAddress("*****@*****.**", "ADMIN 303 NO-REPLY"); var toAddress = new MailAddress(um.Email, um.UserName); const string fromPassword = "******"; const string subject = "Reset Password"; string body = ("Your new password is: " + recover); um.Password = recover; UnicodeEncoding uEncode = new UnicodeEncoding(); byte[] data = uEncode.GetBytes(um.Password); data = new System.Security.Cryptography.SHA512Managed().ComputeHash(data); um.Password = Convert.ToBase64String(data); dal.EditUser(um); var smtp = new SmtpClient { Host = "smtp.gmail.com", Port = 587, EnableSsl = true, DeliveryMethod = SmtpDeliveryMethod.Network, Credentials = new NetworkCredential(fromAddress.Address, fromPassword), Timeout = 20000 }; using (var message = new MailMessage(fromAddress, toAddress) { Subject = subject, Body = body }) { smtp.Send(message); } //dal.InsertUser(userModel); Session["message"] = "Reset password email is successfully sent"; Session["requestStatus"] = "Success"; return(RedirectToAction("Index", "Login")); } catch (Exception ex) { Session["message"] = "Unable to perform this request"; Session["requestStatus"] = "Error"; return(RedirectToAction("ResetPassword", "Login")); } }