• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
聞道有先后,术业有专攻
习惯成自然
博客园    首页    新随笔    联系   管理    订阅  订阅

ASP.NET 防止用户跳过登陆界面

1.在登陆页面的检查登陆成功代码后添加如下代码
Session["UserID"] = txtUserID.Text.Trim();
或者
Session["UserID"] = "OK";
例如,在我的 Login.aspx.cs 的登陆按钮响应事件中代码如下:
if (BaseClass.CheckUser(txtUserID.Text.Trim(), txtPwd.Text.Trim()))
{
Session["UserID"] = txtUserID.Text.Trim();
Response.Redirect("Main.aspx");
}
else
{
Response.Write("<script>alert('用户名或密码错误');location='Login.aspx'</script>");
}
2.然后在每个页面的Page_Load的开始处添加如下代码:
if (Session["UserID"] == null || Session["UserID"].ToString() == "")
{
Response.Write("<Script language='javascript'>window.top.location.href='Login.aspx';</Script>");
}
例如在我的 Main.aspx.cs 的Page_Load代码包含如下内容:
protected void Page_Load(object sender, EventArgs e)
{
if (Session["UserID"] == null || Session["UserID"].ToString() == "")
{
Response.Write("<Script language='javascript'>window.top.location.href='Login.aspx';</Script>");
}
if (!IsPostBack)
{
}
}
本文来自: 脚本之家(http://www.jb51.net/) 详细出处参考:http://www.jb51.net/article/17526.htm
posted @ 2009-03-31 15:20  张立余  阅读(706)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3