Exemple #1
0
    protected void UserLogin_Click(object sender, EventArgs e)
    {
        //对用户输入进行编码
        string username = Server.HtmlEncode(UserName.Text.Trim());
        string password = Server.HtmlEncode(Password.Text.Trim());
        UserData userdata = new UserData();
        IList<User> userlist = userdata.GetUserByUserName(username);
        if (userlist.Count >= 1 && password == userlist.First().Password)
        {
            string usertype = userlist.First().SuperUser == 1 ? "管理员" : "会员";
            string userid = userlist.First().UserId.ToString();
            LabelSuccessOrNot.Text = "登录成功,请稍后...";
            //写会话对象
            Session["UserName"] = username;
            Session["UserType"] = usertype;
            Session["UserId"] = userid;

            //写Cookies
            Response.Cookies["UserInfo"]["UserName"] = username;
            Response.Cookies["UserInfo"]["UserType"] = usertype;
            Response.Cookies["UserInfo"]["UserId"] = userid;
            Response.Cookies["UserInfo"].Expires = DateTime.Now.AddDays(1);

            //已经登录,显示用户界面
            this.Panel1.Visible = false;
            this.Panel2.Visible = true;
            if (usertype == "管理员")
            {
                Manager.Visible = true;
            }
            GetProductNum();
        }
        else
            LabelSuccessOrNot.Text = "登录失败";
    }