我正在使用FormsAuthentication进行用户登录。用户成功注销后我遇到了问题,后退按钮是浏览器允许用户查看页面。我尝试使用javascript
<script type = "text/javascript" >
function preventBack() { window.history.forward(1); }
setTimeout("preventBack()", 0);
window.onunload = function () { null };
</script>
但是后退按钮已完全禁用。它工作了,我不想在用户登录时禁用后退按钮功能。我希望我的登录用户正常使用浏览器后退按钮。但是一旦他选择注销,他就不能通过按"返回"来查看任何内容。我也尝试使用
Session.Abandon();
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetExpires(DateTime.Now);
但这也不起作用。我该如何解决这个问题?
您可以在用户注销时清除浏览器历史记录:
var url = window.location.href;
window.history.go(-window.history.length);
window.location.href = url;
然而,这不会特别健壮 - 它依赖于javascript,它不能跨多个选项卡工作,并且可能只会惹恼用户。IMO 最好的办法是设置适当的缓存标头,以便浏览器不会通过适当应用的 NoCacheAttribute 缓存任何"登录"页面:
public class NoCacheAttribute : ActionFilterAttribute
{
public override void OnResultExecuting(ResultExecutingContext filterContext)
{
filterContext.HttpContext.Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1));
filterContext.HttpContext.Response.Cache.SetValidUntilExpires(false);
filterContext.HttpContext.Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
filterContext.HttpContext.Response.Cache.SetCacheability(HttpCacheability.NoCache);
filterContext.HttpContext.Response.Cache.SetNoStore();
base.OnResultExecuting(filterContext);
}
}
在需要控制后退按钮的 html 页面中使用此代码。
$().ready(function() {
if(document.referrer != 'http://localhost:8181/'){
history.pushState(null, null, 'login');
window.addEventListener('popstate', function () {
history.pushState(null, null, 'login');
});
}
});
此代码将阻止后退按钮事件。if 条件用于在上一页为"http://localhost:8181/"时允许后退按钮。如果上一页不是"http://localhost:8181/",则后退按钮将不起作用。如果您需要阻止所有以前的页面,请避免使用 if 条件。history.pushState 语句会将浏览器地址栏上的 url 替换为"login"。因此,我建议您使用页面URL更改"登录"。
这种方法的优点:-
- 无需控制缓存。
- 我们可以允许指定先前页面的后退按钮事件,并可以阻止其余页面。
希望我的回答能帮助某人。
禁用后退按钮不是满足您需求的正确方法。相反,您可以在 html 文件中添加以下三个标签,以负责清除缓存。
<META Http-Equiv="Cache-Control" Content="no-cache">
<META Http-Equiv="Pragma" Content="no-cache">
<META Http-Equiv="Expires" Content="0">
我发现最简单的方法是使用 OutputCache 属性
[OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]
public class HomeController : Controller
{
}
<script language="JavaScript" type="text/javascript">
window.history.forward();
</script>
请仔细阅读文章 http://www.aspdotnet-suresh.com/2011/11/disable-browser-back-button.html。我在我的布局页面中使用了作者提供的javacript函数来防止后退按钮问题,因为我需要向我网站的所有访问者提供对某些页面的访问权限。
这个解决方案在IE 11和Chrome版本43.0.2357.130 m中对我有用。
希望这有帮助。
var url = window.history.forward();
window.history.go(-window.history.length);
如果你想让你的所有页面都这样做,你可以在你的Global.asax中写:
protected void Application_BeginRequest()
{
Response.AddHeader("Cache-Control", "no-cache, no-store, must-revalidate");
Response.AddHeader("Pragma", "no-cache");
Response.AddHeader("Expires", "0");
}
这不会缓存您网站的任何页面。
请在母版页加载事件中使用此代码。
if(!IsPostBack)
{
if (Session["LoginId"] == null)
Response.Redirect("frmLogin.aspx");
else
{
Response.ClearHeaders();
Response.AddHeader("Cache-Control", "no-cache, no-store, max-age=0, must-revalidate");
Response.AddHeader("Pragma", "no-cache");
}
}
希望对您有所帮助! :)