密码更改失败。请更正错误,然后重试。

时间:2012-03-30 12:35:56

标签: c# asp.net-mvc-3 razor change-password

运行我的应用程序时无法更改密码。它始终坚持验证摘要,我无法更改我的密码。代码中是否需要更改或配置?这是我收到的错误。

  

密码更改失败。请更正错误,然后重试。   当前密码不正确或新密码无效。

但我确信我正确输入了当前密码。

MODEL AccountModel.cs

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Web.Mvc;
using System.Web.Security;

namespace Customer.Models
{    
    public class ChangePasswordModel
    {
        [Required]
        [DataType(DataType.Password)]
        [Display(Name = "Current password")]
        public string OldPassword { get; set; }

        [Required]
        [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
        [DataType(DataType.Password)]
        [Display(Name = "New password")]
        public string NewPassword { get; set; }

        [DataType(DataType.Password)]
        [Display(Name = "Confirm new password")]
        [Compare("NewPassword", ErrorMessage = "The new password and confirmation password do not match.")]
        public string ConfirmPassword { get; set; }
    }
}

控制器

// GET: /Account/ChangePassword
[Authorize]
public ActionResult ChangePassword()
{
    return View();
}

// POST: /Account/Register
[HttpPost]
public ActionResult Register(RegisterModel model)
{
    if (ModelState.IsValid)
    {
        // Attempt to register the user
        //MembershipCreateStatus createStatus;

        try
        {
            MembershipService.CreateUser(model.UserName, model.FullName, model.Password, model.Email);

            //if (createStatus == MembershipCreateStatus.Success)
            //{
                FormsAuthentication.SetAuthCookie(model.UserName, false /* createPersistentCookie */);
                return RedirectToAction("Index", "Home");
            //}
        }
        catch(ArgumentException ae)
        {
            ModelState.AddModelError("", ae.Message);
        }
    }

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

// POST: /Account/ChangePassword
[Authorize]
[HttpPost]
public ActionResult ChangePassword(ChangePasswordModel model)
{
    if (ModelState.IsValid)
    {
        // ChangePassword will throw an exception rather
        // than return false in certain failure scenarios.
        bool changePasswordSucceeded;

        try
        {
            MembershipUser currentUser = Membership.GetUser(User.Identity.Name, true);
            changePasswordSucceeded = currentUser.ChangePassword(model.OldPassword, model.NewPassword);
        }
        catch (Exception)
        {
            changePasswordSucceeded = false;
        }

        if (changePasswordSucceeded)
        {
            return RedirectToAction("ChangePasswordSuccess");
        }
        else
        {
            ModelState.AddModelError("", "The current password is incorrect or the new password is invalid.");
        }
    }

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


// GET: /Account/ChangePasswordSuccess
public ActionResult ChangePasswordSuccess()
{
    return View();
}

1 个答案:

答案 0 :(得分:1)

验证工作正常,这就是为什么你得到“当前密码不正确或新密码无效”。在您的错误报告中。你的会员提供商正在发生一些事情,导致你的旗帜不真实......调试到这个地方

changePasswordSucceeded = currentUser.ChangePassword(model.OldPassword, model.NewPassword);

让我知道你找到了什么!