在不离开视图的情况下验证凭证

本文关键字:情况下 验证 凭证 视图 不离 离开 | 更新日期: 2023-09-27 18:09:02

我有一个用户登录页面,我想检查插入的凭据是否正确。这里是控制器:

编辑:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SustIMS.Models;
namespace SustIMS.Controllers
{
    public class MainController : Controller
    {
        public static string userName;
        public static string password;
        public ActionResult Index()
        {
            getCredentials();
            if (Authenticate(userName, password))
            {
                return View();
            }
            ModelState.AddModelError("", "Could not authenticate");
            return Redirect("../Home");
        }

        public void getCredentials()
        {
            if (Request["username"] != null && Request["password"] != null)
            {
                userName = Request["username"].ToString();
                password = Request["password"].ToString();
            }
        }
    }
}

如果插入的凭证正确,则一切正常:Authenticate函数验证,如果它返回true,则ActionResult返回View

但是,如果它们不正确,我将其设置为返回null,而转到空白页。

我希望它留在同一页面上,并显示某种消息给用户通知凭据不正确(通过显示div,调用javascript函数与弹出窗口,…,我不知道)。

我该怎么做呢?

感谢

编辑

这是我的观点:

@{
    Layout = null;
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <title>@ViewBag.Message</title>
    @Styles.Render("~/Content/css")
    @Scripts.Render("~/bundles/modernizr")
    @Scripts.Render("~/bundles/jquery")
</head>
<body>
    <div id="outer-limit">
        <div class="logo-main">
            <a href="../Home/Index">
                <img class="displayed" src="../../Images/logo.png" alt="SustIMS" /></a>
        </div>
        <section class="container">
            @Html.Partial("LoginForm")
            @Html.ValidationSummary()
        </section>
    </div>
</body>
</html>

和部分LoginForm:

<div class="login">
    <h1>@ViewBag.Login</h1>
    <form method="get" action="../Main/Index">
    <p>
        <input type="text" name="username" value="" placeholder="Username" maxlength="30"></p>
    <p>
        <input type="password" name="password" value="" placeholder="Password" maxlength="25"></p>
    <br />
    <br />
    <p class="submit">
        <input type="submit" onclick="Refresh()" name="commit" value="Login"></p>
    </form>
</div>

在不离开视图的情况下验证凭证

这样做应该可以工作。检查ModelState可以/将通过视图上的Attributes验证所需的字段(如果您已经实现了它们),否则您可以在操作中手动处理。在返回视图之前添加模型错误将导致错误在页面重新加载后显示在页面上。验证成功后,我们将重定向到另一个操作。

public ActionResult Index(LoginModel model) {
  if (ModelState.IsValid) {
    if (AuthenticateUser(model)) {
      return RedirectToAction("LoggedIn");
    } else {
      ModelState.AddModelError("", "Could not authenticate"); //or better error
    }
  }
  return View(model);
}

一定要在视图中添加一个@Html.ValidationSummary(),以显示返回的错误消息。

你应该能够调整上面的代码来使用你的方法,但最终,我强烈建议你输入你的视图,并通过post传递一个模型。

编辑根据你上面的代码重新做了我的例子,而不是一个一般的例子

public ActionResult Index()
{
    getCredentials();
    if (Authenticate(userName, password))
    {
        return View();
    }
    ModelState.AddModelError("", "Could not authenticate");
    return View();
}

我的建议是在验证成功后返回RedirectToAction,而不是返回相同的视图。

一种方法:

public ActionResult Index(string url)
    { 
        getCredentials();
        if (Authenticate(userName, password))
        {
          return View(); 
        }
        else
        {
          TempData["ErrorMessage"]="Wrong Credential";
          return Redirect(url);
        } 
       }
    }

然后在你的视图中显示消息在一个div中,如果它不是空的

@if(TempData["ErrorMessage"] != null)
{
  <div class='alert alert-success alert-dismissable' >
  @TempData["ErrorMessage"] 
  </div>
}

我相信你有错误的想法。NET MVC工作。如果提交表单,则会发出完整的新请求,并显示其响应—也就是说,必须返回一些内容以显示给用户—通常是视图。永远不要返回ActionResult。在您的例子中,您在两种情况下都返回View(),只是给它不同的数据来显示—例如成功消息或错误消息。你可以使用强类型模型,或者ViewBag -看看一些教程。

如果您希望用户在凭据错误时"停留在同一页面上",则需要某种ajax验证。ASP。. NET MVC也支持这一点,看看http://msdn.microsoft.com/en-us/library/gg508808(vs.98).aspx

相关文章: