Esempio n. 1
0
        public void Verify()
        {
            const string newPassword = "******";
            const string oldPassword = "******";
            const string username = "******";
            var changePasswordModel = new ChangePasswordModel
            {
                CurrentPassword = oldPassword,
                NewPassword = newPassword,
                ConfirmPassword= newPassword,
            };

            var manageUserPage = Host
                .Instance
                .NavigateToInitialPage<HomePage>()
                .Login(new LoginModel {Username = username, Password = oldPassword})
                .TopMenu
                .GoToChangePasswordPage()
                .ChangePassword(changePasswordModel);

            manageUserPage.HasSuccessMessage().ShouldBe(true);

            manageUserPage
                .Menu
                .LogOff()
                .Login(new LoginModel {Username = username, Password = newPassword})
                .TopMenu
                .IsLoggedOn
                .ShouldBe(true);
        }
Esempio n. 2
0
        public virtual async Task<ActionResult> ChangePassword(ChangePasswordModel model)
        {
            bool hasPassword = HasPassword();
            ViewBag.HasLocalPassword = hasPassword;
            ViewBag.ReturnUrl = Url.Action("ChangePassword");
            if (hasPassword)
            {
                if (ModelState.IsValid)
                {
                    var userId = _userManager.FindByName(User.Identity.Name).Id;
                    IdentityResult result = await _userManager.ChangePasswordAsync(userId, model.CurrentPassword, model.NewPassword);
                    if (result.Succeeded)
                    {
                        return RedirectToAction("ChangePassword", new { Message = ManageMessageId.ChangePasswordSuccess });
                    }
                    
                    AddErrors(result);
                }
            }
            else
            {
                // User does not have a password so remove any validation errors caused by a missing CurrentPassword field
                ModelState state = ModelState["CurrentPassword"];
                if (state != null)
                {
                    state.Errors.Clear();
                }

                if (ModelState.IsValid)
                {
                    var userId = _userManager.FindByName(User.Identity.Name).Id;
                    IdentityResult result = await _userManager.AddPasswordAsync(userId, model.NewPassword);
                    if (result.Succeeded)
                    {
                        return RedirectToAction("ChangePassword", new { Message = ManageMessageId.SetPasswordSuccess });
                    }
                    
                    AddErrors(result);
                }
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }