Ejemplo n.º 1
0
        public ActionResult ForgotPassword(AccountModel model, FormCollection fc)
        {
            db_KISDEntities _Context = new db_KISDEntities();

            #region Send Email
            if (!string.IsNullOrEmpty(model.Email))
            {
                bool   IsMailSent = false;
                string AppPath    = ConfigurationManager.AppSettings["AppPath"].ToString();
                string body       = string.Empty;
                var    query      = _Context.Users.Where(x => x.EmailTxt.Trim().ToLower() == model.Email.Trim().ToLower() && x.IsDeletedInd == false).FirstOrDefault();
                if (query != null)
                {
                    string FullLink         = AppPath + "/Reset/ResetPassword?u=" + EncryptDecrypt.Encrypt(query.UserID.ToString());
                    var    myMailUtilityBAL = new MailUtilityBAL();
                    var    FromEmail        = ConfigurationManager.AppSettings["FromEmail"].ToString();

                    #region Email Body
                    body  = "<table style=\"font-family:Arial;font-size:12px\" border=\"0\" align=\"left\" cellpadding=\"4\" cellspacing=\"0\" >";
                    body += "<tr><td colspan='2' > Dear " + query.FirstNameTxt + " " + query.LastNameTxt + @",<br/><br/>Please click on the <a href='" + FullLink + "'>link</a> to reset your Password.</td></tr>";
                    body  = body + "<tr><td colspan='2'><br/>Regards,<br/>Killeen ISD Team.</td></tr></table>";
                    #endregion

                    #region Email
                    try
                    {
                        if (myMailUtilityBAL.SendEmail(FromEmail, query.EmailTxt, body, ("Killeen ISD - Password Reset Request")).ToLower().Trim() == "ok")
                        {
                            IsMailSent = true;
                        }
                    }
                    catch (Exception ce)
                    {
                        IsMailSent = false;
                    }

                    #endregion
                }
                if (IsMailSent)
                {
                    model.Message = "Reset Password link is sent to the email. Please click on the link and reset the Password.";
                }
            }
            #endregion

            return(RedirectToAction("ForgotPassword", model));
        }
Ejemplo n.º 2
0
        public ActionResult ResetPassword(ResetPasswordModel model, string command)
        {
            if (string.IsNullOrEmpty(command))
            {
                if (ModelState.IsValid)
                {
                    // ChangePassword will throw an exception rather
                    // than return false in certain failure scenarios.
                    bool changePasswordSucceeded = false;
                    try
                    {
                        if (model.NewPassword == model.ConfirmPassword) // && Regex.Match(model.NewPassword, @"^.*(?=.{8,20})(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!@#$%^&+=]).*$").Success
                        {
                            try
                            {
                                var  password   = model.NewPassword;
                                var  md5HashNew = GetMd5Hash(model.NewPassword);
                                var  objContext = new db_KISDEntities();
                                long UserID     = model.UserID;
                                var  obj        = objContext.Users.Where(x => x.UserID == UserID).FirstOrDefault();
                                obj.PasswordTxt = md5HashNew;
                                objContext.SaveChanges();

                                #region email
                                var myMailUtilityBAL = new MailUtilityBAL();
                                var FromEmail        = ConfigurationManager.AppSettings["FromEmail"].ToString();
                                var body             = "";
                                var query            = objContext.Users.Where(x => x.UserID == UserID && x.IsDeletedInd == false).FirstOrDefault();
                                if (query != null && query.EmailTxt != null)
                                {
                                    #region Email Body
                                    body  = "<table style=\"font-family:Arial;font-size:12px\" border=\"0\" align=\"left\" cellpadding=\"4\" cellspacing=\"0\" >";
                                    body += "<tr><td colspan='2' > Dear " + query.FirstNameTxt + " " + query.LastNameTxt + @",<br/><br/>Your password has been updated successfully.Please see below your login details:</td></tr>";
                                    body += "<tr><td colspan='2' > <br/>Email: " + query.EmailTxt + @"</td></tr>";
                                    body += "<tr><td colspan='2' > User Name: " + query.UserNameTxt + @"</td></tr>";
                                    body += "<tr><td colspan='2' > Password: "******"</td></tr>";
                                    body  = body + "<tr><td colspan='2'><br/><b>Regards,<br/>Killeen ISD Team.</b></td></tr></table>";
                                    #endregion

                                    try
                                    {
                                        if (myMailUtilityBAL.SendEmail(FromEmail, query.EmailTxt, body, ("Killeen ISD - Password Reset Confirmation")).ToLower().Trim() == "ok")
                                        {
                                            changePasswordSucceeded = true;
                                        }
                                    }
                                    catch
                                    {
                                        changePasswordSucceeded = false;
                                    }
                                }
                                else
                                {
                                    changePasswordSucceeded = false;
                                }
                                #endregion

                                changePasswordSucceeded = true;
                                return(RedirectToAction("ResetConfirmation", "Reset"));
                            }
                            catch
                            {
                                changePasswordSucceeded = false;
                            }
                        }
                        //else if (!Regex.Match(model.NewPassword, @"^.*(?=.{8,20})(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!@#$%^&+=]).*$").Success)
                        //{
                        //    TempData["AlertMessage"] = "Password must be 8 to 20 alphanumeric characters including one uppercase letter, one lowercase letter and one special character.";
                        //    return View(model);
                        //}
                        else
                        {
                            TempData["AlertMessage"] = "Confirm New Password should be same as New Password.";
                            return(View(model));
                        }
                    }
                    catch (Exception)
                    {
                        changePasswordSucceeded = false;
                    }
                }

                // If we got this far, something failed, redisplay form
                return(View(model));
            }
            else
            {
                RouteValueDictionary rvd = new RouteValueDictionary();
                rvd.Add("cus", model.UserID);
                model.NewPassword     = "";
                model.ConfirmPassword = "";
                return(RedirectToAction("ResetPassword", "Reset", rvd));
            }
        }