【问题标题】:Password change was unsuccessful. Please correct the errors and try again.密码更改失败。请更正错误并重试。
【发布时间】:2012-03-30 12:35:56
【问题描述】:

我在运行应用程序时无法更改密码。它总是卡在验证摘要上,我无法更改我的密码。代码上是否有需要更改或配置的内容?这是我收到的错误。

密码更改失败。请更正错误并重试。 当前密码错误或新密码无效。

但我确定我输入的当前密码正确。

型号 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();
}

【问题讨论】:

  • 有什么例外吗?模型返回为有效吗?
  • 我可以登录我的应用程序,我可以创建、更新用户。但是当我尝试使用该代码更改密码时,我似乎无法更改它。
  • @MapelCatalan 您需要准确描述正在发生的事情,有什么错误吗? ModelState.Isvalid = true/false 吗?
  • @DanielA.White 逗号“ModelState.IsValid”无效。
  • “ModelState.IsValid”无效。

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


【解决方案1】:

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

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

告诉我你发现了什么!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-13
    • 1970-01-01
    • 1970-01-01
    • 2018-08-26
    • 2014-08-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多