【问题标题】:How to change the default error message "Incorrect password." in the Manage Account of MVC5?如何更改默认错误消息“密码错误”。在 MVC5 的管理帐户中?
【发布时间】:2014-07-22 02:36:45
【问题描述】:

也许这是一个简单的问题,但我已经厌倦了寻找它。

我在 AccountViewModels.cs 中有这个(这是一个默认的 MVC5 项目)。

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

我知道这使用了后面的数据注释,但是当我输入不正确的 OldPassword 时,我不知道如何更改默认错误消息,现在的默认错误消息是 Incorrect password. 而我想换成另一条消息,请帮帮我。

【问题讨论】:

    标签: c# asp.net asp.net-mvc-5


    【解决方案1】:

    这个很简单不用着急,去AccountController.cs中的

     // POST: /Account/Manage
     [HttpPost]
     [ValidateAntiForgeryToken]
     public async Task<ActionResult> Manage(ManageUserViewModel model)
     {
    

    ….

    改变这个:

    if (hasPassword)
    {
         if (ModelState.IsValid)
         {
              IdentityResult result = await UserManager.ChangePasswordAsync(User.Identity.GetUserId(), model.OldPassword, model.NewPassword);
              if (result.Succeeded)
              {
                   return RedirectToAction("Manage", new { Message = ManageMessageId.ChangePasswordSuccess });
              }
              else
              {
                   AddErrors(result);
              }
         }
    }
    

    为此:

    if (hasPassword)
    {
         if (ModelState.IsValid)
         {
              IdentityResult result = await UserManager.ChangePasswordAsync(User.Identity.GetUserId(), model.OldPassword, model.NewPassword);
              if (result.Succeeded)
              {
                   return RedirectToAction("Manage", new { Message = ManageMessageId.ChangePasswordSuccess });
              }
              else
              {
                   ModelState.AddModelError(string.Empty, "Whatever message do you want to say");
              }
         }
    }
    

    就是这样!

    编辑:代码审查。

    【讨论】:

      猜你喜欢
      • 2017-03-09
      • 1970-01-01
      • 2018-06-04
      • 1970-01-01
      • 2018-12-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-22
      相关资源
      最近更新 更多