/// <summary> /// 验证用户登录,登录成功则返回该用户对象,失败返回null /// </summary> /// <param name="user">要验证的用户对象</param> /// <returns>返回用户对象,登录失败返回null</returns> public Users CheckLogin(Users user) { Users returnUser = null; string sql = "select * from tb_user where user_name=@loginName and user_password=@password"; SqlParameter[] values = new SqlParameter[]{ new SqlParameter("@loginName",user.LoginName), new SqlParameter("@password",user.Password) }; DataTable dt = DBHelper.GetDataTable(sql, values); if (dt.Rows.Count > 0) { DataRow dr = dt.Rows[0]; returnUser = new Users(); returnUser.LoginName = user.LoginName; returnUser.Password = user.Password; returnUser.Id = Convert.ToInt32(dr["id"]); } return returnUser; }
protected void btnLogin_Click(object sender, EventArgs e) { string name = txtName.Text; string pwd = txtPwd.Text; Users user = new Users(); user.LoginName = name; user.Password = pwd; UsersService concate = new UsersService(); user = concate.CheckLogin(user); if (user == null) lblMessage.Text = "用户名或登录错误,登录失败"; else { Session["Users"] = user; lblMessage.Text = "登录成功"; Response.Redirect("../后台首页.aspx");//页面跳转 } }