Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the default error message "Incorrect password." in the Manage Account of MVC5?

Maybe this is an easy question but I'm tired of searching for it.

I have this in the AccountViewModels.cs (this is a default MVC5 project).

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

I know this uses Data Annotations behind, but I don't know how to change the default error message when I enter the incorrect OldPassword, the default error message right now is Incorrect password. and I want to change it for another message, please help me.

like image 442
mejiamanuel57 Avatar asked Jul 22 '14 02:07

mejiamanuel57


1 Answers

This is simple don’t worry, go to AccountController.cs in the

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

….

Change this :

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);
          }
     }
}

For this:

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");
          }
     }
}

That’s it!

EDIT: code review.

like image 125
jcruzz92 Avatar answered Nov 12 '22 14:11

jcruzz92