コード例 #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 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 ChangePasswordContract;

            if (model == null)
            {
                filterContext.Controller.ViewData.ModelState.AddModelError("", @"Invalid update 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" +
                                                                           @" must be different");
                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");
                return;
            }

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

            if (changePassword == null)
            {
                filterContext.Controller.ViewData.ModelState.AddModelError("", "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 update password" : changePassword.Status.Message.FriendlyMessage);
                return;
            }


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