mvc3登录表单用户名密码错误提示

sen*_*ale 3 c# asp.net-mvc-3

[HttpPost]
        public ActionResult Login(LoginModel model)
        {
            if (ModelState.IsValid)
            {
                Authenticate(model.Username, model.Password);
                return RedirectToAction("Index");
            }
            else
            {
                return View(model);
            }
        }

        private void Authenticate(string userName, string password)
        {
            const string commaSeperatedRoles = "Administrator,Editor";

            if (userName == "xx" && password == "xxx")
            {
                FormsAuthenticationUtil.RedirectFromLoginPage(userName, commaSeperatedRoles, false);
            }
        }
Run Code Online (Sandbox Code Playgroud)

和登录模型

public class LoginModel
    {
        [Required(ErrorMessage="*")]
        [StringLength(10)]
        public string Username { get; set; }

        [Required(ErrorMessage = "*")]
        [StringLength(10)]
        public string Password { get; set; }
    }
Run Code Online (Sandbox Code Playgroud)

并查看

@using (Html.BeginForm())
{
    <table width="100%">
    <tr>
        <td>Username:</td>
        <td>
            @Html.TextBoxFor(m => m.Username, new { @style = "width: 140px;" })
        </td>
    </tr>
    <tr>
        <td>Passwd:</td>
        <td>
            @Html.PasswordFor(m => m.Password, new { @style = "width: 140px;" })
        </td>
    </tr>
    </table>
    <div class="napaka">Wrong username and password</div>
    <br />
    <input type="submit" class="button" value="Login" />
}
Run Code Online (Sandbox Code Playgroud)

但现在我总是收到错误登录的消息“用户名和密码错误”。仅当用户名和密码错误时如何写此消息?我在 C# 中使用 mvc3。我可以以某种方式发送 bool 变量来查看或者 sbet 方式是什么?

Dar*_*rov 5

如果凭据错误,请向模型状态添加错误消息:

[HttpPost]
public ActionResult Login(LoginModel model)
{
    if (ModelState.IsValid)
    {
        if (Authenticate(model.Username, model.Password))
        {
            // authentication was successful. The method created the cookie
            // so we can safely redirect to an authenticated page here
            return RedirectToAction("Index");
        }
        else
        {
            // authentication failed due to wrong username or password =>
            // we add an error message to the ModelState here so that we 
            // can show it in the view
            ModelState.AddModelError("", "Wrong username and password");
        }
    }

    // At this stage we know that some error happened =>
    // we redisplay the view so that the user can fix it
    return View(model);
}

private bool Authenticate(string userName, string password)
{
    const string commaSeperatedRoles = "Administrator,Editor";
    if (userName == "xx" && password == "xxx")
    {
        // Warning: you should not be redirecting here. You should only
        // create and set the authentication cookie. The redirect should be done
        // in an MVCish way, i.e. by returning a RedirectToAction result if this
        // method returns true
        FormsAuthenticationUtil.SetAuthCookie(userName, commaSeperatedRoles, false);
        return true;
    }

    // wrong username or password
    return false;
}
Run Code Online (Sandbox Code Playgroud)

并在视图中使用 ValidationSummary 帮助器,而不是在 div 中硬编码错误消息:

@using (Html.BeginForm())
{
    <table width="100%">
    <tr>
        <td>Username:</td>
        <td>
            @Html.TextBoxFor(m => m.Username, new { @style = "width: 140px;" })
        </td>
    </tr>
    <tr>
        <td>Passwd:</td>
        <td>
            @Html.PasswordFor(m => m.Password, new { @style = "width: 140px;" })
        </td>
    </tr>
    </table>

    @Html.ValidationSummary(false)
    <br />
    <input type="submit" class="button" value="Login" />
}
Run Code Online (Sandbox Code Playgroud)