protected void register_Click(object sender, EventArgs e) { string name = userName.Text.Trim(); string pwd = userPwd.Text.Trim(); string secondPwd = rePwd.Text.Trim(); if (pwd != secondPwd) { warmMsg.Text = "两次密码输入不一致,请重新输入!"; return; } if (string.IsNullOrEmpty(name)) { warmMsg.Text = "用户名不能为空!"; return; } if (string.IsNullOrEmpty(pwd)) { warmMsg.Text = "密码不能为空!"; return; } //新建用户 UserModel userModel = new UserModel(); userModel.UserName = name; userModel.Password = pwd; bool isExist = userDataAccess.CheckUserExist(userModel); if (isExist) { warmMsg.Text = "用户名已存在,请重新输入!"; return; } userModel = userDataAccess.InsertUser(userModel); if (userModel.ID > 0) { warmMsg.Text = "注册成功!"; } }
/// <summary> /// 用户登陆流程 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void login_Click(object sender, EventArgs e) { string name = username.Text.Trim(); string pwd = userPwd.Text.Trim(); if (string.IsNullOrEmpty(name)) { ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "<script>alert('用户名不能为空,请重新输入!')</script>"); return; } if (string.IsNullOrEmpty(pwd)) { ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "<script>alert('密码不能为空,请重新输入!')</script>"); return; } UserModel userModel = new UserModel(); userModel.UserName = name; //检察用户名是否存在 bool isExist = userAccess.CheckUserExist(userModel); if (!isExist) { ClientScript.RegisterClientScriptBlock(this.GetType(),"alert","<script>alert('用户不存在,请重新输入帐号信息!')</script>"); return; } userModel = userAccess.GetUserByName(name); if (userModel.Password == pwd) { //如果有returnUrl 则反回returnUrl string returnUrl = Request.QueryString.Get("returnUrl"); Session["UserName"] = userModel.UserName; Response.Redirect(string.IsNullOrEmpty(returnUrl) ? "/index.aspx" : returnUrl); } else { ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "<script>alert('帐号或密码有误,请重试!')</script>"); } }
/// <summary> /// 检察用户名是否已存在 /// </summary> /// <param name="user">需要检察的用户模型</param> /// <returns>用户是否存在的Bool</returns> public bool CheckUserExist(UserModel user) { //根据用户名称查找用户信息 UserModel userTemp = GetUserByName(user.UserName); //如果用户ID>0说明用户存在 if (userTemp.ID > 0) { return true; } return false ; }
/// <summary> /// 添加新用户,如果添加成功则反回新用户模型,如果添加失败则反回空用户模型 /// </summary> /// <param name="user">要添加的新用户模型</param> /// <returns></returns> public UserModel CreateUser(UserModel user) { UserModel newUser = new UserModel(); //检察用户是否已存在 bool userExist = CheckUserExist(user); if (userExist) { return newUser; } //如果用户不存在则允许添加到数据库 newUser.UserName = user.UserName; newUser.Password = user.Password; newUser.Role = user.Role; newUser.UserLevel = user.UserLevel; //写入数据库 return newUser; }
/// <summary> /// 根据用户名查找到用户相关信息,如果用户ID为空则说明用户不存在 /// </summary> /// <param name="userName">用户名</param> /// <returns>UserModel</returns> public UserModel GetUserByName(string userName) { UserModel user = new UserModel(); using (SqlConnection connection = new SqlConnection(ConnectionString)) { string sqlStr = "use NetBar select ID,Password,Role,UserLevel,UserName from UserTable where UserName = @UserName"; SqlCommand cmd = new SqlCommand(sqlStr, connection); cmd.Parameters.AddWithValue("@UserName", userName); connection.Open(); using (SqlDataReader reader = cmd.ExecuteReader()) { while (reader.Read()) { user.ID =Convert.ToInt32(reader["Id"]); user.Password = Convert.ToString(reader["Password"]); user.Role = Convert.ToString(reader["Role"]); user.UserLevel = Convert.ToInt32(reader["UserLevel"]); user.UserName = Convert.ToString(reader["UserName"]); } return user; } } }
/// <summary> /// 添加新用户,如果成功刚产生用户ID /// </summary> /// <param name="user"></param> /// <returns></returns> public UserModel InsertUser(UserModel user) { using (SqlConnection connection = new SqlConnection(ConnectionString)) { connection.Open(); string sqlStr = "use NetBar insert into UserTable (Password,Role,UserLevel,UserName) values (@Password,@Role,@UserLevel,@UserName)"; SqlCommand cmd = new SqlCommand(sqlStr, connection); cmd.Parameters.AddWithValue("@Password", user.Password); cmd.Parameters.AddWithValue("@Role", user.Role); cmd.Parameters.AddWithValue("@UserLevel", user.UserLevel); cmd.Parameters.AddWithValue("@UserName", user.UserName); int res = (int)cmd.ExecuteNonQuery(); if (res > 0) { //添加成功 user = GetUserByName(user.UserName); } return user; } }