单击后退按钮停止重定向页面

时间:2013-08-26 11:32:27

标签: c# javascript jquery asp.net .net

我正在尝试删除特定网站的历史记录。当我注销页面然后按后退按钮时,页面会在上一页中移动,但我希望当任何用户注销然后按下后退按钮时,它将位于登录页面的同一页面上,而不是上一页。我正在尝试所有方法,如会话放弃,缓存删除但我的问题没有解决。我们也可以使用JavaScript。

protected void btnLogout_Click(object sender, EventArgs e)
{
    HttpContext.Current.Cache.Remove("");
    HttpContext.Current.Session.Clear();
    HttpContext.Current.Session.Abandon();
    Response.Redirect("../Login.aspx");
} 

6 个答案:

答案 0 :(得分:1)

您可以尝试使用javascript:

 <script type="text/javascript">
    //Logout clears all visited pages for Back Button
    function noBack() { window.history.forward(); }
    noBack();
    window.onload = noBack;
    window.onpageshow = function (evt) { if (evt.persisted) noBack(); }
    window.onunload = function () { void (0); }
</script>

答案 1 :(得分:0)

如果使用缓存提供页面,请执行以下操作以设置正确的http标头

HttpContext.Current.Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1));
HttpContext.Current.Response.Cache.SetValidUntilExpires(false);
HttpContext.Current.Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
HttpContext.Current.Response.Cache.SetNoStore();

答案 2 :(得分:0)

您有两种选择:

阻止缓存页面 阻止用户在使用JavaScript注销后返回上一页。

我会建议第一个,但如果它不适合你,请按照提到的第二个选项here

答案 3 :(得分:0)

从我看来,看起来你正在处理由会话变量登录的人。如果是这种情况,那么在母版页或基页中,您无法通过检查是否存在某个会话变量来检查以确保该用户已登录?如果他们已登录,请继续正常,否则您可以将他们重定向回登录页面(或默认)以使他们登录?

答案 4 :(得分:0)

在后面的代码中添加以下代码

protected override void OnPreRender(EventArgs e)
    {
        base.OnPreRender(e);
        string strDisAbleBackButton;
        strDisAbleBackButton = "<script language=\"javascript\">\n";
        strDisAbleBackButton += "window.history.forward(1);\n";
        strDisAbleBackButton += "\n</script>";
        ClientScript.RegisterClientScriptBlock(this.Page.GetType(), "clientScript", strDisAbleBackButton);
    }
    protected void Page_Init(object Sender, EventArgs e)
    {
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.Cache.SetExpires(DateTime.Now.AddSeconds(-1));
        Response.Cache.SetNoStore();
    }

希望上面的代码适合你。

答案 5 :(得分:-1)

这个问题在堆栈溢出中被多次询问过。简单的搜索可以为您提供解决方案。 注销后,您必须禁用后退按钮。因为您必须使用

Page.Response.Cache.SetCacheability(HttpCacheability.NoCache)

删除缓存本身将停止向后导航。

相关问题