1.Login.aspx登录将登录名赋值入 Session[\"username\"]

protected void btnLogin_Click( sender, EventArgs e)
        {
            string username = txtUserName.Value.Trim();
            string userpassword = txtUserPassword.Value.Trim();

            UserEntity model = new UserEntity();
            model.Username = username;
            model.Password = Utility.MD5(userpassword);

            int i = LoginBLL.Login(model);

            Response.Write(i);

            if (i>0)
            {
                HttpContext.Current.Session[\"username\"] = username;
                HttpContext.Current.Session.Timeout = 7200;
                Response.Redirect(\"Index.aspx\");
            }
            else 
            {
                if (i == -1)
                {
                    lab_errorMsg.InnerText = \"没有此账户!!!\";
                    txtUserName.Value = \"\";
                    txtUserPassword.Value = \"\";
                }
                else
                {
                    txtUserName.Value = \"\";
                    txtUserPassword.Value = \"\";
                    lab_errorMsg.InnerText = \"账户或密码错误!!!\";
                }
                
            }
        }

 

2.Index.aspx首页判断Session[\"username\"]是否为空,为空跳回Login.aspx

 protected void Page_Load( sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (System.Web.HttpContext.Current.Session[\"username\"] != null)
                {
                    IndexDataBind();
                }
                else
                {
                    Response.Redirect(\"Login.aspx\");
                }
            }
        }

 

收藏 打印