Пример #1
0
            public override void OnActionExecuting(ActionExecutingContext filterContext)
            {
                filterContext.Controller.ViewBag.UpdateSucceed = "0";
                if (!filterContext.Controller.ViewData.ModelState.IsValid)
                {
                    filterContext.Controller.ViewData.ModelState.AddModelError("", "Invalid update information");
                    return;
                }

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

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

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

                if (model == null)
                {
                    filterContext.Controller.ViewData.ModelState.AddModelError("", "Invalid update information");
                    return;
                }
                if (
                    string.Compare(model.OldPassword.Trim(), model.NewPassword.Trim(),
                                   StringComparison.InvariantCultureIgnoreCase) == 0)
                {
                    filterContext.Controller.ViewData.ModelState.AddModelError("", "Old Password and New Password and must be different");
                    return;
                }

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

                string msg;

                if (!UserAuthentication.ChangeFirstLoginPassword(model.UserName, model.OldPassword, model.NewPassword, out msg))
                {
                    filterContext.Controller.ViewData.ModelState.AddModelError("", msg.Length > 0 ? msg : "Process Failed! Unable to update password");
                    return;
                }

                var myProfile = MvcApplication.MyUserProfile(model.UserName);

                if (myProfile != null)
                {
                    myProfile.UserFirstLogin = 0;
                    MvcApplication.SetUserProfile(model.UserName, myProfile);
                }
                filterContext.Controller.ViewBag.UpdateSucceed = "1";
                base.OnActionExecuting(filterContext);
            }