c# - MVC5管理账户默认错误信息 "Incorrect password."如何修改?

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

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

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

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

我知道这背后使用了数据注释,但我不知道如何在输入不正确的旧密码时更改默认错误消息,现在默认错误消息是不正确的密码。和我想更改为另一条消息,请帮助我。

最佳答案

这很简单不用担心,转到

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

就是这样!

编辑:代码审查。

关于c# - MVC5管理账户默认错误信息 "Incorrect password."如何修改?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24878000/

相关文章:

c# - 为什么 Select 返回一个 bool 值?

c# - 绑定(bind)到静态类属性和 StringFormat

C# Dll 导入失败 : "The application has failed to start because its side-by-side configuration is incorrect"

c# - 是什么导致 RoleEnvironment 初始值设定项中的 ModuleLoadException?

c# - IIS 10.0 最大请求长度 .NET MVC

c# - Angular ASP.NET MVC 绑定(bind)

asp.net - 是否可以获取正在运行应用程序的应用程序池(事先不知道网站)?

c# - 如何使用 OWIN 中间件组件检查 MVC 响应流?

html - 隐藏小屏幕中的菜单项和下拉菜单 Bootstrap 3

c# - 强制将 excel 窗口带到前面?