用户登出后如何停止返回按钮? (LOGOUT按钮在主页面上) 使用webforms
我有几个页面,最后一个页面是最终页面,在注销后,当我点击后退按钮时,它显示上一页。 我该如何避免这种情况。请帮我提供代码。
代码仅需要在LOGOUT之后触发。 用户必须能够返回并查看以前的页面,以便在登录状态下进行任何更改。
您应该设置正确的HTML头。根据这里所述,这些是在所有浏览器上都有效的头部:
Cache-Control: no-cache, no-store, must-revalidate
Pragma: no-cache
Expires: 0
您可以像这样进行设置:
HttpContext.Current.Response.AddHeader("Cache-Control", "no-cache, no-store, must-revalidate");
HttpContext.Current.Response.AddHeader("Pragma", "no-cache");
HttpContext.Current.Response.AddHeader("Expires", "0");
Page_Load
中工作)。 - Patrick Hofman在浏览器中按返回按钮时仍能完美运行(在Chrome中测试过)
我使用了一个会话变量,还禁用了缓存
页面1(default.aspx)是登录表单,我在其中收集会话变量ID
Session["id"] = TextBox1.Text;// just the username</pre>
page 2(Default2.aspx) in page load
protected void Page_Load(object sender, EventArgs e)
{
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetExpires(DateTime.Now.AddDays(-1));
Response.Cache.SetNoStore();
if (Session["id"] == null)
{
Response.Redirect("Default.aspx");// redirects to page1(default.aspx)
}
else
{
Label1.Text = (string)Session["id"];//just display user name in label
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Session["id"] = null;
Response.Redirect("/WebSite5/Default.aspx");//redirects to page1(default.aspx)
}
<script>
function preventBack(){window.history.forward();}
setTimeout("preventBack()", 0);
window.onunload=function(){null};
</script>
它可能适用于你
window.onunload = function() {
window.location.href = "<%=request.getContextPath()%>/logout.jsp";
};
function preventBack(){window.history.forward();}
setTimeout("preventBack()", 0);
window.onunload=function(){null};
我知道这是一个旧的帖子,但是有人可能会需要帮助。您可以使用JavaScript事件windows.hash change来防止返回按钮。
<script>
var history_api = typeof history.pushState !== 'undefined';
if (location.hash == '#no-back') {
if (history_api) history.pushState(null, '', '#stay')
else location.hash = '#stay'
window.onhashchange = function () {
if (location.hash == '#no-back') {
alert('Action cannot be done!');
if (history_api) history.pushState(null, '', '#stay')
else location.hash = '#stay'
}
}
}
</script>
你可以点击这里查看完整的教程
尝试这个方法,我发现这是防止后退的最佳方式。在codeproject中找到了这个解决方案。
使用这些常见方法在注销时清除会话值。
Session.Abandon();
Session.Clear();
Session.RemoveAll();
System.Web.Security.FormsAuthentication.SignOut();
Response.Redirect("frmLogin.aspx", false);
<script type="text/javascript">
$(document).ready(function () {
CheckingSeassion();
});
function CheckingSeassion() {
$.ajax({
type: "POST",
url: "frmLogout.aspx/LogoutCheck",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
if (response.d == 0) {
window.location = '<%= BaseURL %>' + "frmLogin.aspx";
}
},
failure: function (msg) {
alert(msg);
}
});
}
LogoutCheck() WebMethod会在客户端加载时检查应用程序服务器上的会话值。
我在frmLogout.aspx页面上创建了这个方法,如下所示:
[WebMethod]
public static int LogoutCheck()
{
if (HttpContext.Current.Session["user"] == null)
{
return 0;
}
return 1;
}
这种方法仅适用于MVC网站
这是我使用的方法
将以下过滤器添加到您的FilterConfig中(假设您正在使用MVC)
filters.Add(new System.Web.Mvc.AuthorizeAttribute());
现在,使用装饰器来修饰那些未登录用户也可以使用的操作方法。
[AllowAnonymous]
一定要装饰你的登录和注册方法。
现在,当用户点击返回按钮时(假设页面刷新),他们将被要求再次登录。 我不允许我的页面被缓存,强制刷新返回按钮。
希望这能对你有所帮助。
Webforms
还是MVC
?我的想法是在你想要保护的页面上检查用户是否已登录(例如这个“上一页”)。@PatrickHofman答案中的链接指向一个结果,说明后退按钮历史记录不是缓存。 - TheGeekZnResponse.Cache.SetCacheability(HttpCacheability.NoCache); Response.Cache.SetExpires(DateTime.Now);
和/或HTML代码,例如<meta http-equiv="cache-control" content="max-age=0" /> <meta http-equiv="cache-control" content="no-cache" /> <meta http-equiv="expires" content="0" /> <meta http-equiv="expires" content="Tue, 01 Jan 1980 1:00:00 GMT" /> <meta http-equiv="pragma" content="no-cache" />
。但是JavaScript是最可行的选项。 - Bhavik