[英]ASP.NET MVC logout and back button
我在 ASP.NET MVC 中有一個應用程序,用 C# 編寫,其中用戶login
。 然后出現一個屏幕,您可以在其中看到您的東西並有注銷按鈕 (cerrar sesión)。 登錄和注銷按鈕有效,但問題在於后退按鈕,當我按下它時,您會看到以下內容:
然后我按F5 :
當按下繼續時,登錄的用戶頁面將重新加載:
換句話說,當您返回 go 時,您會看到要再次發送表單的空白頁面,當您按繼續時,再次顯示用戶的視圖。 有沒有辦法避免它? 目前我的代碼在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>
全球.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();
}
在 Global.asax.cs 文件中添加以下 function。
protected void Application_BeginRequest()
{
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1));
Response.Cache.SetNoStore();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.