如何从一个选项卡注销并影响其余选项卡?
How to logout from one tab and affect the rest of the tabs?
我有一个问题,假设我有一个控制器:
-Login -Index -Ventana random -LogOff
Ventana random 在新选项卡中打开,因此我们将有两个选项卡(Index 和 Ventana random),我如何从 Ventana random 选项卡创建它,当按下注销按钮时,索引选项卡 returns 到登录屏幕 ?
控制器:
public ActionResult Index()
{
return View("Index");
}
public ActionResult Login(string uname, string psw)
{
Session["uname"] = uname;
Response.AppendHeader("Cache-Control", "no-store");
if(uname == null || psw == null)
{
return View("Login");
}
else
{
try
{
ConexionSQL sql = new ConexionSQL();
var usuario = sql.login(uname, psw);
var existencia = sql.usuarioOCobrador(uname, psw);
if (existencia[0].usuario == "USUARIO")
{
var caracteres = Convert.ToString(psw);
var pruebaUsuario = sql.datos(uname);
if (pruebaUsuario[0].clave == caracteres)
{
if (pruebaUsuario[0].nivel == 8 //nada
|| pruebaUsuario[0].nivel == 9
|| pruebaUsuario[0].nivel == 11
|| pruebaUsuario[0].nivel == 12
|| pruebaUsuario[0].nivel == 13
|| pruebaUsuario[0].nivel == 15)
{
ViewBag.Usu = "Usted no cuenta o no tiene los permisos suficientes para ingresar al sistema. Comuníquese con su Departamento.";
}
else
{
return View("Index", usuario);
}
}
else
{
ViewBag.Contra = "Contraseña incorrecta.";
}
}
else
{
ViewBag.Usu = "Usuario incorrecto o usted no tiene los permisos para ingresar al sistema. Comuníquese con su Departamento.";
}
}
catch (SqlException ex)
{
throw ex;
}
}
return View("Login");
}
[NoCache]
public ActionResult LogOff()
{
FormsAuthentication.SignOut();
Session.Clear();
Session.RemoveAll();
Session.Abandon();
return RedirectToAction("Login", "Home");
}
public class NoCacheAttribute : ActionFilterAttribute
{
public override void OnResultExecuting(ResultExecutingContext filterContext)
{
if (filterContext == null) throw new ArgumentNullException("filterContext");
var cache = GetCache(filterContext);
cache.SetExpires(DateTime.UtcNow.AddDays(-1));
cache.SetValidUntilExpires(false);
cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
cache.SetCacheability(HttpCacheability.NoCache);
cache.SetNoStore();
base.OnResultExecuting(filterContext);
}
protected virtual HttpCachePolicyBase GetCache(ResultExecutingContext filterContext)
{
return filterContext.HttpContext.Response.Cache;
}
}
在索引视图中我有这个按钮:
<button class="btn btn-outline-danger my-2 my-sm-0" onclick="window.open('VentanaRandom.html')" type="button">Open new tab</button>
这会使用此按钮为我打开一个新标签页:
<form class="form-inline my-2 my-lg-0 ml-auto" method="post" action="@Url.Action("LogOff", "Home")">
<button class="btn btn-outline-danger my-2 my-sm-0" type="submit">Cerrar sesión</button>
</form>
现在我已经打开了两个选项卡,如何让第一个带有索引的选项卡在按下注销按钮后从第二个选项卡转到登录屏幕?
如果您添加一个像 IsStillLoggedIn
这样的控制器操作来检查您是否仍然拥有有效的 cookie(或您使用的任何身份验证方案),也许您可以在文档中使用 visibilitychange
事件JavaScript - 像这样:
function ensureStillLoggedIn() {
if (document.visibilityState === "visible") {
// Tab is now active, check if we're still logged in
fetch("/Home/IsStillLoggedIn", { credentials: "same-origin" })
.then(response => {
if (response === "false") {
window.location = "/Home/Login";
}
});
}
}
document.addEventListener("visibilitychange", ensureStillLoggedIn);
然后 运行 这个 JavaScript 每当页面加载时。这样,用户在切换到该选项卡时应该会注销。
我有一个问题,假设我有一个控制器:
-Login -Index -Ventana random -LogOff
Ventana random 在新选项卡中打开,因此我们将有两个选项卡(Index 和 Ventana random),我如何从 Ventana random 选项卡创建它,当按下注销按钮时,索引选项卡 returns 到登录屏幕 ?
控制器:
public ActionResult Index()
{
return View("Index");
}
public ActionResult Login(string uname, string psw)
{
Session["uname"] = uname;
Response.AppendHeader("Cache-Control", "no-store");
if(uname == null || psw == null)
{
return View("Login");
}
else
{
try
{
ConexionSQL sql = new ConexionSQL();
var usuario = sql.login(uname, psw);
var existencia = sql.usuarioOCobrador(uname, psw);
if (existencia[0].usuario == "USUARIO")
{
var caracteres = Convert.ToString(psw);
var pruebaUsuario = sql.datos(uname);
if (pruebaUsuario[0].clave == caracteres)
{
if (pruebaUsuario[0].nivel == 8 //nada
|| pruebaUsuario[0].nivel == 9
|| pruebaUsuario[0].nivel == 11
|| pruebaUsuario[0].nivel == 12
|| pruebaUsuario[0].nivel == 13
|| pruebaUsuario[0].nivel == 15)
{
ViewBag.Usu = "Usted no cuenta o no tiene los permisos suficientes para ingresar al sistema. Comuníquese con su Departamento.";
}
else
{
return View("Index", usuario);
}
}
else
{
ViewBag.Contra = "Contraseña incorrecta.";
}
}
else
{
ViewBag.Usu = "Usuario incorrecto o usted no tiene los permisos para ingresar al sistema. Comuníquese con su Departamento.";
}
}
catch (SqlException ex)
{
throw ex;
}
}
return View("Login");
}
[NoCache]
public ActionResult LogOff()
{
FormsAuthentication.SignOut();
Session.Clear();
Session.RemoveAll();
Session.Abandon();
return RedirectToAction("Login", "Home");
}
public class NoCacheAttribute : ActionFilterAttribute
{
public override void OnResultExecuting(ResultExecutingContext filterContext)
{
if (filterContext == null) throw new ArgumentNullException("filterContext");
var cache = GetCache(filterContext);
cache.SetExpires(DateTime.UtcNow.AddDays(-1));
cache.SetValidUntilExpires(false);
cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
cache.SetCacheability(HttpCacheability.NoCache);
cache.SetNoStore();
base.OnResultExecuting(filterContext);
}
protected virtual HttpCachePolicyBase GetCache(ResultExecutingContext filterContext)
{
return filterContext.HttpContext.Response.Cache;
}
}
在索引视图中我有这个按钮:
<button class="btn btn-outline-danger my-2 my-sm-0" onclick="window.open('VentanaRandom.html')" type="button">Open new tab</button>
这会使用此按钮为我打开一个新标签页:
<form class="form-inline my-2 my-lg-0 ml-auto" method="post" action="@Url.Action("LogOff", "Home")">
<button class="btn btn-outline-danger my-2 my-sm-0" type="submit">Cerrar sesión</button>
</form>
现在我已经打开了两个选项卡,如何让第一个带有索引的选项卡在按下注销按钮后从第二个选项卡转到登录屏幕?
如果您添加一个像 IsStillLoggedIn
这样的控制器操作来检查您是否仍然拥有有效的 cookie(或您使用的任何身份验证方案),也许您可以在文档中使用 visibilitychange
事件JavaScript - 像这样:
function ensureStillLoggedIn() {
if (document.visibilityState === "visible") {
// Tab is now active, check if we're still logged in
fetch("/Home/IsStillLoggedIn", { credentials: "same-origin" })
.then(response => {
if (response === "false") {
window.location = "/Home/Login";
}
});
}
}
document.addEventListener("visibilitychange", ensureStillLoggedIn);
然后 运行 这个 JavaScript 每当页面加载时。这样,用户在切换到该选项卡时应该会注销。