如何阻止用户在注销后访问以前的页面
本文关键字:访问 何阻止 用户 注销 | 更新日期: 2023-09-27 18:30:26
这里的代码将 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");
}
用户注销后如何阻止访问以前的页面
有几种方法
使用Session.Abandon
清除Session
并使用Response.Redirect("~/LoginPage.aspx");
然后,您可以使用以下方法清除缓存或清除历史记录
使用代码隐藏
// 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);