/// <summary> /// 用户登录 /// </summary> /// <param name="model">传递有用户名和密码的实体,若存在此用户(登录成功),其实体对象将对带回</param> /// <returns>1:登录成功 -1:用户名或密码错误</returns> public int user_Login(VwUserModel model) { LoginDal dal = new LoginDal(); VwUserModel userModel = dal.getByUserCodeAndPwd(model.UserCode, model.Password); int code = -1; if (userModel != null) { // 如果存在此用户,则将其赋值给形参,方便调用者获取此用户详细信息,利用引用类型特点 // 注意,不能直接这样赋值,这样直接改变了一会形参的指针,不再指向实参,而是指向方法内 studentModel //model = studentModel; model.Id = userModel.Id; model.UserName = userModel.UserName; model.Telephone = userModel.Telephone; model.IdCard = userModel.IdCard; model.Gender = userModel.Gender; // 注意,返回的是加密后的密码 model.Password = userModel.Password; model.Dept = userModel.Dept; model.RoleId = userModel.RoleId; // 标记存在此用户,密码 code = 1; } return(code); }
public VwUserModel getByUserCodeAndPwd(string userCode, string password) { //string sql = $@"SELECT Id // , UserCode // , UserName // , Password // , Gender // , IDCard // , Telephone // , Dept // , RoleId // FROM vw_userInfo where UserCode='{userCode}' AND Password='******'"; string sql = @"SELECT Id , UserCode , UserName , Password , Gender , IDCard , Telephone , Dept , RoleId FROM vw_userInfo where UserCode=@UserCode AND Password=@Password"; // 注意,不要再给参数加 引号 '',因为最后程序会为参数值加'' // 错误: UserCode='@UserCode' #region 参数化后执行SQL //exec sp_executesql N'SELECT Id // , UserCode // , UserName // , Password // , Gender // , IDCard // , Telephone // , Dept // , RoleId // FROM vw_userInfo where UserCode = @UserCode AND Password = @Password',N'@UserCode nvarchar(50),@Password nvarchar(50)',@UserCode=N'170010347',@Password=N'21232f297a57a5a743894a0e4a801fc3' #endregion SqlParameter[] pars = new SqlParameter[] { new SqlParameter("@UserCode", SqlDbType.NVarChar, 50), new SqlParameter("@Password", SqlDbType.NVarChar, 50) }; pars[0].Value = userCode; pars[1].Value = password; DataTable dt = MSSQL.query(sql, pars); VwUserModel model = null; if (dt.Rows.Count > 0) { model = Utils.dataTable2List <VwUserModel>(dt)[0]; } return(model); }
private void autoLogin(VwUserModel model) { if (!string.IsNullOrEmpty(Request["isAutoLogin"])) { // 保存7天--账号密码在Cookie HttpCookie cookieUserCode = new HttpCookie("UserCode", model.UserCode); cookieUserCode.Expires = DateTime.Now.AddDays(7); HttpCookie cookieUserPwd = new HttpCookie("UserPwd", model.Password); cookieUserPwd.Expires = DateTime.Now.AddDays(7); Response.Cookies.Add(cookieUserCode); Response.Cookies.Add(cookieUserPwd); } }
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { if (!loginStateCheck()) { Response.Redirect("/Account/Login.aspx"); } VwUserModel vwUserModel = ((VwUserModel)Session["User"]); this.UserName = vwUserModel.UserName; this.MenuList = this.sysFuncBll.getByRoleId(vwUserModel.RoleId); } }
protected void btnSave_Click(object sender, EventArgs e) { this.model = new BookTicketInfoExModel(); this.model.TrainNumber = this.txtTrainNumber.Text.Trim(); this.model.StartStation = this.txtStartStation.Text.Trim(); this.model.EndStation = this.txtEndStation.Text.Trim(); if (DateTime.TryParse(this.txtBookDate.Text.Trim(), out DateTime bookDate)) { this.model.BookDate = bookDate; } else { this.Message = "预定日期格式不正确"; return; } this.model.Phone = this.txtPhone.Text.Trim(); this.model.Remark = this.txtRemark.Text.Trim(); if (Request["type"] == "add") { // 新增 // 获取当前登录账户 VwUserModel vwUserModel = (VwUserModel)Session["User"]; // 当前登录学生的票,所以该页面只允许普通学生或学生管理员登录 model.StudentId = vwUserModel.Id; // 最后的操作人这里也算作 该申请学生 if (bookTicketInfoBll.bookTicket(model, vwUserModel.Id)) { this.Message = "订票成功"; initControl(); } else { this.Message = "订票失败"; } } else if (Request["type"] == "edit") { // 编辑 this.model.Id = Convert.ToInt32(Request["id"]); if (bookTicketInfoBll.editModel(model) > 0) { Response.Redirect("BookTicketList.aspx"); } else { this.Message = "修改失败,请稍后再试"; } } }
protected void btnLogin_Click(object sender, EventArgs e) { VwUserModel model = new VwUserModel(); string userCode = Request["userCode"]; string userPwd = Request["userPwd"]; if (string.IsNullOrEmpty(userCode) || string.IsNullOrEmpty(userPwd)) { this.Message = "用户名或密码不能为空"; return; } model.UserCode = userCode; model.Password = MD5Helper.MD5Encrypt32(userPwd); int code = loginBll.user_Login(model); #region MyRegion //int code = -1; //DataTable dt = loginBll.getByUserCodeAndPwd(userCode, model.Password); //int rowCount = dt.Rows.Count; //if (rowCount > 0) //{ // code = 1; // model.UserName = dt.Rows[0]["UserName"].ToString(); // model.Gender = Convert.ToBoolean(dt.Rows[0]["Gender"] == DBNull.Value ? true : dt.Rows[0]["Gender"]); // model.IdCard = dt.Rows[0]["IDCard"].ToString(); // model.Telephone = dt.Rows[0]["Telephone"].ToString(); // model.ClassName = dt.Rows[0]["ClassName"].ToString(); //} #endregion if (code == -1) { this.Message = "用户名或密码错误"; } else if (code == 1) { // 登录成功 Session["User"] = model; autoLogin(model); Response.Redirect(homeUrl); } }
protected void btnReg_Click(object sender, EventArgs e) { #region 二次验证 string ticket = Request.Form["ticket"]; string randstr = Request.Form["randstr"]; TencentCaptchaHelper captcha = new TencentCaptchaHelper("1"); VerifyResult verifyResult = captcha.Verify(ticket, randstr, Request.UserHostAddress); if (verifyResult.Code != 1) { this.Message = "验证已经过期, 或未通过验证,请重新验证<br>详细错误: " + verifyResult.Message; return; } #endregion #region 注册 StudentModel model = new StudentModel(); model.StudentNumber = this.txtStudentNumber.Text.Trim(); model.StudentName = this.txtStudentName.Text.Trim(); model.Telephone = this.txtTelephone.Text.Trim(); model.Password = MD5Helper.MD5Encrypt32(this.txtPwd.Text.Trim()); model.Identification = this.txtIdentification.Text.Trim(); model.ClassName = this.txtClassName.Text.Trim(); model.Gender = this.rblGender.SelectedValue == "1" ? true : false; model.RoleId = 1; StudentBll bll = new StudentBll(); int result = bll.add(model); if (result > 0) { // ?????存在 BUG,这里拿不到Id Session["User"] = new VwUserModel() { Id = 0, UserName = model.StudentName, Password = model.Password, UserCode = model.StudentNumber, Gender = model.Gender, IdCard = model.Identification, RoleId = model.RoleId, Telephone = model.Telephone }; Response.Redirect("/Account/Login.aspx"); } #endregion }
protected bool loginStateCheck() { bool isLogin = true; if (Session["User"] == null) { if (Request.Cookies["UserCode"] != null && Request.Cookies["UserPwd"] != null) { // 是否记住了登录信息 LoginBll loginBll = new LoginBll(); VwUserModel userModel = new VwUserModel() { UserCode = Request.Cookies["UserCode"].Value, Password = Request.Cookies["UserPwd"].Value }; int code = loginBll.user_Login(userModel); if (code == -1) { Response.Cookies["UserCode"].Expires = DateTime.Now.AddDays(-1); Response.Cookies["UserPwd"].Expires = DateTime.Now.AddDays(-1); //Response.Redirect("/Account/Login.aspx"); isLogin = false; } else if (code == 1) { Session["User"] = userModel; } } else { // 未记住登录信息 //Response.Redirect("/Account/Login.aspx"); isLogin = false; } } return(isLogin); }