Example #1
0
        public ActionResult Manage(LocalPasswordModel model)
        {
            bool hasLocalAccount = OAuthWebSecurity.HasLocalAccount(WebSecurity.GetUserId(User.Identity.Name));
            ViewBag.HasLocalPassword = hasLocalAccount;
            ViewBag.ReturnUrl = Url.Action("Manage");
            if (hasLocalAccount)
            {
                if (ModelState.IsValid)
                {
                    // ChangePassword will throw an exception rather than return false in certain failure scenarios.
                    bool changePasswordSucceeded;
                    try
                    {
                        changePasswordSucceeded = WebSecurity.ChangePassword(User.Identity.Name, model.OldPassword, model.NewPassword);
                    }
                    catch (Exception)
                    {
                        changePasswordSucceeded = false;
                    }

                    if (changePasswordSucceeded)
                    {
                        return RedirectToAction("Manage", new { Message = ManageMessageId.ChangePasswordSuccess });
                    }
                    else
                    {
                        ModelState.AddModelError("", "The current password is incorrect or the new password is invalid.");
                    }
                }
            }
            else
            {
                // User does not have a local password so remove any validation errors caused by a missing
                // OldPassword field
                ModelState state = ModelState["OldPassword"];
                if (state != null)
                {
                    state.Errors.Clear();
                }

                if (ModelState.IsValid)
                {
                    try
                    {
                        WebSecurity.CreateAccount(User.Identity.Name, model.NewPassword);
                        return RedirectToAction("Manage", new { Message = ManageMessageId.SetPasswordSuccess });
                    }
                    catch (Exception)
                    {
                        ModelState.AddModelError("", String.Format("Unable to create local account. An account with the name \"{0}\" may already exist.", User.Identity.Name));
                    }
                }
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }
Example #2
0
        public ActionResult ChangePassword(LocalPasswordModel result)
        {
            Person person = reposetory.GetPerson(CurrentProfile.PersonID);

            if (Regex.IsMatch(result.NewPassword, @"^[a-zA-Z0-9æøåÆØÅ*!#%&]{6,10}$") & ModelState.IsValid)
            {
                //bool changePasswordSucceeded;
                try
                {
                    string userName = User.Identity.Name;
                   if (WebSecurity.ChangePassword(userName, result.OldPassword, result.NewPassword))
                    {
                        person.Password = result.NewPassword;
                        reposetory.SavePerson(person);
                        ViewBag.FormSucces = true;
                        ModelState.Clear();
                        result = new LocalPasswordModel();
                    }
                    else
                    {
                        ModelState.AddModelError("", General.ErrorPassword);
                    }
                }
                catch (Exception)
                {
                    ModelState.AddModelError("", General.ErrorPassword);
                }
            }
            else
            {
                ModelState.AddModelError("",
                  General.ErrorPassword);
            }
            return View(result);
        }