验证凭据而不离开视图(Verifying credentials without leaving view)

编程入门 行业动态 更新时间:2024-10-28 02:20:59
验证凭据而不离开视图(Verifying credentials without leaving view)

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

编辑:

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>

I've got a user login page that I want to check if the inserted credentials are correct. Here's the controller:

EDITED:

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

Everything is OK if the inserted credentials are correct: the Authenticate function verifies that and if it returns true, the ActionResult returns the View.

However, if they are not correct, I set it to return null, going to a blank page instead.

I want it to stay on the same page and display some kind of message to the user informing that the credentials aren't correct (by showing a div, calling a javascript function with a popup window, ..., I don't really know).

How can I do that?

Thanks

EDIT

Here's my view:

@{ 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>

And the partial 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>

最满意答案

一种方法:

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

One way to do that :

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

Then in your view Show the message in a div if it's not empty

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

更多推荐

本文发布于:2023-08-07 11:46:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464176.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:凭据   而不   视图   Verifying   leaving

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!