C# 在不离开视图的情况下验证凭据
我有一个用户登录页面,我想检查插入的凭据是否正确。这是控制器: 编辑:C# 在不离开视图的情况下验证凭据,c#,asp.net-mvc,C#,Asp.net Mvc,我有一个用户登录页面,我想检查插入的凭据是否正确。这是控制器: 编辑: 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 {
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
返回视图
但是,如果它们不正确,我将其设置为返回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>
我相信你对ASP.NETMVC的工作原理有错误的想法。如果您提交表单,则会发出整个新请求,并显示其响应——也就是说,您必须返回一些内容以显示回用户视图。永远不要作为ActionResult
返回。在您的情况下,在这两种情况下都返回View()
,只需给它不同的数据来显示,例如,成功消息或错误消息。您可以使用strongle类型的模型进行此操作,也可以使用ViewBag
-查看一些教程
如果您希望用户在凭证错误时“保持在同一页面”,则需要某种ajax验证。ASP.NET MVC也支持此功能,请查看一种实现此功能的方法:
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>
}
@if(TempData[“ErrorMessage”]!=null)
{
@TempData[“ErrorMessage”]
}
这样做应该行得通。检查ModelState可以/将通过视图上的属性验证所需字段(如果您已经实现了这些属性),否则您可以在操作中手动处理这些字段。在返回视图之前添加模型错误将导致错误在重新加载后显示在页面上。成功验证后,我们将重定向到另一个操作
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()
,以显示返回的错误消息
您应该能够定制上面的代码来使用您的方法,但最终,我建议您强烈地键入您的视图,并通过一篇文章传递一个模型
编辑
根据上面的代码重编我的示例,而不是一般示例
public ActionResult Index()
{
getCredentials();
if (Authenticate(userName, password))
{
return View();
}
ModelState.AddModelError("", "Could not authenticate");
return View();
}
我的建议是,在成功身份验证后重定向到操作
,而不是返回相同的视图。这实际上会返回一个名为“错误凭据”的视图,因为实际上没有定义errorMessage,所以它不会编译。的确,我的错误,如果凭证为空,此版本应可在视图中显示消息false@JDahm,这将前进到下一个视图。如果凭证错误,它只会在视图包中携带一个字符串。除非凭据正确,否则我不想转到下一个视图。如果他们错了,我想要么留在同一个视图上,要么在itOk中用一些消息重新加载它,我想你应该转到登录/索引视图。如果我现在理解得很好,假设您在Home/Index上,您可以转到Login/Index,但如果失败,您希望返回Home/Index,并显示一条错误消息?如果成功,请转到login/index当我更新我的答案时,在TempData中设置错误消息,然后重定向到您传递给操作的URL(调用login的页面)TempData在重定向后仍然存在,这样您就可以在页面上获取消息。为什么不改用AJAX呢?您将进行异步调用,但您将无法导航到其他视图,除非您同时发送要导航的url,客户端将导航到该url。这将非常好,但我实际上从未使用过AJAX
——我不知道如何使用。不过我还是会研究一下的,谢谢,很简单。大量文档。我没有任何LoginModel,我将插入的凭据与通过webservice callit获得的数据进行比较。它仍然可以正常工作,您可能只需稍微修改它即可为您的“模型”工作。对不起,我没有任何模型。或者我不明白您的意思,您应该能够在我的示例中将模型错误添加到ModelState add中,并在验证凭据后返回视图。即使你没有使用模型。这是一个你也可以这样做的例子,我在例子中用你的代码更新了我的答案,而不是一个一般的例子
public ActionResult Index()
{
getCredentials();
if (Authenticate(userName, password))
{
return View();
}
ModelState.AddModelError("", "Could not authenticate");
return View();
}