コード例 #1
0
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            filterContext.Controller.ViewBag.IsSuccessful = false;
            if (!filterContext.Controller.ViewData.ModelState.IsValid)
            {
                //filterContext.Controller.ViewData.ModelState.AddModelError("", "Invalid Password Information");
                //filterContext.Controller.ViewBag.Error = "Invalid Password Information";
                filterContext.Controller.ViewData.ModelState.AddModelError("", "Invalid Password Information: New and Confirm Password not match");
                filterContext.Controller.ViewBag.Error = "Invalid Password Information: New and Confirm Password not match";
                return;
            }

            var modelList = filterContext.ActionParameters.Where(ap => ap.Key == "model").ToList();

            if (modelList.IsNullOrEmpty())
            {
                filterContext.Controller.ViewData.ModelState.AddModelError("", "Invalid Password Information");
                filterContext.Controller.ViewBag.Error = "Invalid Password Information";
                return;
            }
            if (!modelList.Any() || modelList.Count != 1)
            {
                filterContext.Controller.ViewData.ModelState.AddModelError("", "Invalid Password Information");
                filterContext.Controller.ViewBag.Error = "Invalid Password Information";
                return;
            }

            var model = modelList[0].Value as ChangePasswordContract;

            if (model == null)
            {
                filterContext.Controller.ViewData.ModelState.AddModelError("", "Invalid Password Information");
                filterContext.Controller.ViewBag.Error = "Invalid Password Information";
                return;
            }
            if (
                string.Compare(model.OldPassword.Trim(), model.NewPassword.Trim(),
                               StringComparison.InvariantCultureIgnoreCase) == 0)
            {
                model.ConfirmPassword = "";
                model.NewPassword     = "";
                model.OldPassword     = "";
                filterContext.Controller.ViewData.ModelState.AddModelError("", "Old Password and New Password cannot be same");
                filterContext.Controller.ViewBag.Error = "Current Password and New Password cannot be same";
                return;
            }

            if (
                string.Compare(model.ConfirmPassword.Trim(), model.NewPassword.Trim(),
                               StringComparison.InvariantCultureIgnoreCase) != 0)
            {
                model.ConfirmPassword = "";
                model.NewPassword     = "";
                model.OldPassword     = "";
                filterContext.Controller.ViewData.ModelState.AddModelError("", "New Password and Confirm New Password must match");
                filterContext.Controller.ViewBag.Error = "New Password and Confirm New Password must match";
                return;
            }


            var changePassword = PortalUser.ChangePassword(model.UserName, model.OldPassword, model.NewPassword);

            if (changePassword == null)
            {
                filterContext.Controller.ViewData.ModelState.AddModelError("", "Process Failed! Unable to change password");
                filterContext.Controller.ViewBag.Error = "Process Failed! Unable to change password";
                return;
            }
            if (!changePassword.Status.IsSuccessful)
            {
                filterContext.Controller.ViewData.ModelState.AddModelError("", string.IsNullOrEmpty(changePassword.Status.Message.FriendlyMessage) ? "Process Failed! Unable to change your password" : changePassword.Status.Message.FriendlyMessage);
                filterContext.Controller.ViewBag.Error = string.IsNullOrEmpty(changePassword.Status.Message.FriendlyMessage) ? "Process Failed! Unable to change your password" : changePassword.Status.Message.FriendlyMessage;
                return;
            }


            filterContext.Controller.ViewBag.IsSuccessful = true;
            base.OnActionExecuting(filterContext);
        }