如何在用户退出后阻止用户访问之前的页面

时间:2014-11-15 10:24:20

标签: c# asp.net

这里的代码是使用ASP.NET和C#。问题是,当用户单击“注销”按钮时,用户可以返回上一页。

徽标代码

  protected void Page_Load(object sender, EventArgs e)
    {
        Session["email"] = txtemail.Text;
    }

    protected void btlogin_Click(object sender, EventArgs e)
    {


    SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ConnectionString);
    SqlCommand cmd = conn.CreateCommand();
    cmd.CommandText = "SELECT [email], [password] FROM [customer] WHERE [email]=@email AND [password]=@password";
    cmd.Parameters.Add("@email", SqlDbType.VarChar).Value = txtemail.Text;
    cmd.Parameters.Add("@password", SqlDbType.VarChar).Value = txtpassword.Text;

    conn.Open();
    SqlDataReader reader = cmd.ExecuteReader();

    if (reader.Read())
    {

        Response.Redirect("~/Booking.aspx");

        reader.Close();
        conn.Close();

    }
    else
    {

        lb.Text="Email or Password incorrect";

    }

}

}

退出代码

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["email"] == null)
        {

            Response.Redirect("Default.aspx");

        }
    }

    protected void btlogout_Click(object sender, EventArgs e)
    {
        Session["email"] = null;

        Response.Redirect("Default.aspx");
    }

如何阻止用户在他/她退出后访问之前的页面

1 个答案:

答案 0 :(得分:1)

有几种方法

使用Session清除Session.Abandon并使用Response.Redirect("~/LoginPage.aspx");

然后您可以使用以下方法清除缓存或清除历史记录

使用Codebehind

// Code disables caching by browser.
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1));
Response.Cache.SetNoStore();

使用JavaScript

<SCRIPT LANGUAGE="javascript">
function ClearHistory()
{
     var backlen = history.length;
     history.go(-backlen);
     window.location.href = loggedOutPageUrl
}
</SCRIPT>

使用asp.net

没有更新面板

Page.ClientScript.RegisterStartupScript(this.GetType(),
Guid.NewGuid().ToString(),"ClearHistory();",true);

更新面板

ScriptManager.RegisterStartupScript(this,this.GetType(),
Guid.NewGuid().ToString(),"ClearHistory();",true);
相关问题