【发布时间】:2020-11-21 12:38:09
【问题描述】:
我有一个用 C# 编写的 ASP.NET MVC 应用程序,其中用户 login。然后出现一个屏幕,您可以在其中看到您的东西并有注销按钮 (cerrar sesión)。登录和注销按钮有效,但问题在于后退按钮,当我按下它时,您会看到以下内容:
然后我按F5:
当按下继续时,登录的用户页面将重新加载:
换句话说,当您返回时,您会看到要再次发送表单的空白页面,当您按继续时,用户的视图会再次显示。有没有办法避免它?目前我的代码在controller:
public ActionResult Index()
{
Response.AppendHeader("Cache-Control", "no-store");
return View();
}
[OutputCache(NoStore = true, Duration = 0, Location = OutputCacheLocation.None)]
public ActionResult Login(string uname, string psw)
{
Response.AppendHeader("Cache-Control", "no-store");
try
{
ConexionSQL sql = new ConexionSQL();
var caracteres = Convert.ToString(psw);
var usuario = sql.login(uname, psw);
if (caracteres == "" || caracteres == null || caracteres.Equals(""))
{
ViewBag.Alert = "Ingrese contraseña.";
}
if (usuario.Count <= 0)
{
ViewBag.MensajeUsuario = "El usuario es inexistente.";
}
else if (usuario[0].nivel == 0
|| usuario[0].nivel == 1
|| usuario[0].nivel == 2
|| usuario[0].nivel == 7)
{
return View("Login1", usuario);
}
else if (usuario[0].nivel == 3
|| usuario[0].nivel == 4
|| usuario[0].nivel == 16)
{
return View("Login2");
}
else
{
return View("Index");
}
}
catch (SqlException ex)
{
throw ex;
}
return View("");
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult LogOff()
{
Response.AppendHeader("Cache-Control", "no-store");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetExpires(DateTime.Now.AddSeconds(-1));
Response.Cache.SetNoStore();
Session.Clear();
FormsAuthentication.SignOut();
//return View("Index");
return RedirectToAction("Index", "Home");
}
这是logout button (cerrar sesión)所在视图的代码:
@model IEnumerable<ProvidusHomeWeb.Models.Usuarios>
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<meta charset="utf-8">
<title>Providus</title>
</head>
<body>
@* Navigation Bar *@
<div class="topnav">
<a href="javascript:ruta()">Home</a>
@using (Html.BeginForm("LogOff", "Home", FormMethod.Post, new { role = "form" }))
{
@Html.AntiForgeryToken()
<button onclick="document.getElementById('id01').style.display='block'" style="width:auto;">Cerrar sesión</button>
}
@foreach (var item in Model)
{
<label>Bienvenida/o: @Html.DisplayFor(modelitem => item.usuario)</label>
}
</div>
</body>
</html>
这是login 表单所在的索引视图:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width" />
<title>Providus</title>
</head>
<body>
@* Navigation Bar *@
<div class="topnav">
<a href="javascript:ruta()">Home</a>
<div class="login-container">
<button onclick="document.getElementById('id01').style.display='block'" style="width:auto;">Login</button>
</div>
</div>
<div id="id01" class="modal">
<form class="modal-content animate" onsubmit="return control()" method="post" action="@Url.Action("Login", "Home")">
<div class="imgcontainer">
<span onclick="document.getElementById('id01').style.display='none'" class="close" title="Close Modal">×</span>
<img src="~/Images/00.png" alt="Avatar" class="avatar">
</div>
<div class="container">
<label for="uname"><b>Usuario:</b></label>
<input type="text" id="uname" placeholder="Ingrese usuario..." name="uname" onkeypress="return soloLetras(event)">
<label for="psw"><b>Contraseña:</b></label>
<input type="password" placeholder="Ingrese contraseña..." id="psw" name="psw" onkeypress="return soloNumeros(event)">
<button type="submit">Iniciar sesión</button>
</div>
</form>
</div>
<center><img class="img" src="~/Images/00.png" /></center>
<center><p>Bienvenida/o, por favor inicie sesión.</p></center>
<script>
function control() {
if (document.getElementById('uname').value == null
|| document.getElementById('uname').value == "") {
alert("El campo no puede estar vacío.");
document.getElementById('uname').focus();
return false;
}
else if (document.getElementById('psw').value == null || document.getElementById('psw').value == "") {
alert("El campo no puede estar vacío.");
document.getElementById('psw').focus();
return false;
}
return true;
}
</script>
<center>
<p>@ViewBag.Usu</p>
<p>@ViewBag.Contra</p>
</center>
</body>
</html>
Global.asax:
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
protected void Application_BeginRequest()
{
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1));
Response.Cache.SetNoStore();
}
【问题讨论】:
标签: c# html asp.net-mvc authentication logout