/// <summary> /// 通过用户名获取该用户的密码和盐值 /// </summary> /// <param name="userName"></param> /// <returns></returns> //public DataTable GetPwdAndSalt(string userName) //{ // string sqlStr = "select [password],[salt] from [user] where userName=@userName"; // SqlParameter[] parameters = new SqlParameter[] { // new SqlParameter("@userName",userName) // }; // return SqlHelper.ExecuteDataTable(sqlStr,parameters); //} public User_model GetPwdAndSaltModel(string userName) { string sqlStr = "select [User_Password],[User_Salt] from [tb_User] where User_Name=@userName"; SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@userName", userName) }; DataTable dt = SqlHelper.ExecuteDataTable(sqlStr, parameters); if (dt.Rows.Count > 1) { //数据库出错处理,数据库里存在大于两条用户名一样的数据,抛出异常 throw new Exception("more than 1 row was found"); } if (dt.Rows.Count <= 0) { return(null); } DataRow row = dt.Rows[0]; //把取回来的dt_User表的一行数据转化为model User_model model = new User_model(); model.password = (string)row["User_Password"]; model.salt = (string)row["User_Salt"]; return(model); }
/// <summary> /// 返回特定咨询页面 /// </summary> /// <returns></returns> public ActionResult Consult() { if (Session["username"] == null) { return(View("loginremind")); } string userName = Session["username"].ToString(); LoginStatusConfig();//配置登录状态 User_bll user_bll = new User_bll(); int userId = Convert.ToInt32(user_bll.GetUserId(userName)); //通过用户名获取userId CountUser_model countUser_model = user_bll.GetCountUser(userId); //获取用户的CountUser_model 数据:点赞数 关注数 粉丝数 收藏数 提问数 回帖数 特定咨询数 等 ViewData["countUser_model"] = countUser_model; List <ExpertUserConsult_model> expertUserConsult_modelList = user_bll.GetExpertConsult(); //获取专家的ExpertUserConsult_model数据,用户填写特定咨询时,需要选择专家 User_model user_model = new User_model(); try { user_model = user_bll.GetUserDataConsult(userName);//用户的个人数据 } catch (Exception e) { return(Content(e.ToString())); } ViewData["user_model"] = user_model; return(View(expertUserConsult_modelList)); }
/// <summary> /// 通过用户名获取用的个人资料《特定咨询》 /// </summary> /// <param name="userName"></param> /// <returns></returns> public User_model GetUserDataConsult(string userName) { User_model user_model = new User_model(); string sqlStr = "select User_BirthDate,User_Height,User_SkinColor,User_Weight,User_XiongWei,User_YaoWei,User_TunWei from tb_User where User_Name=@userName"; SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@userName", userName) }; DataTable userData = SqlHelper.ExecuteDataTable(sqlStr, parameters); if (userData.Rows.Count == 0) {//不存在该用户时 throw new Exception("不存在该用户,查询到的数据为空"); } if (userData.Rows[0]["User_BirthDate"] == System.DBNull.Value) { } else { user_model.birthDate = (DateTime)userData.Rows[0]["User_BirthDate"]; } user_model.height = userData.Rows[0]["User_Height"] == System.DBNull.Value ? 0 : Convert.ToSingle(userData.Rows[0]["User_Height"]); user_model.tunWei = userData.Rows[0]["User_TunWei"] == System.DBNull.Value ? 0 : Convert.ToSingle(userData.Rows[0]["User_TunWei"]); user_model.yaoWei = userData.Rows[0]["User_YaoWei"] == System.DBNull.Value ? 0 : Convert.ToSingle(userData.Rows[0]["User_YaoWei"]); user_model.xiongWei = userData.Rows[0]["User_XiongWei"] == System.DBNull.Value ? 0 : Convert.ToSingle(userData.Rows[0]["User_XiongWei"]); user_model.weight = userData.Rows[0]["User_Weight"] == System.DBNull.Value ? 0 : Convert.ToSingle(userData.Rows[0]["User_Weight"]); user_model.skinColor = userData.Rows[0]["User_SkinColor"] == System.DBNull.Value ? "请选择" : userData.Rows[0]["User_SkinColor"].ToString(); return(user_model); }
/// <summary> /// 通过用户名获取用的个人资料《特定咨询》 /// </summary> /// <param name="userName"></param> /// <returns></returns> public User_model GetUserDataConsult(string userName) { User_dal user_dal = new User_dal(); User_model user_model = new User_model(); try { user_model = user_dal.GetUserDataConsult(userName); } catch (Exception e) { throw new Exception(e.ToString()); } DateTime today = DateTime.Now; //今天日期 DateTime birthDate = user_model.birthDate; //出生年月日 int age = today.Year - birthDate.Year; //年龄 if (birthDate > today.AddYears(-age)) //还未生日,年龄减去1 { age--; } user_model.age = age; return(user_model); }
/// <summary> /// 将从数据库里取回的一行数据转化为User_model数据 /// </summary> /// <param name="row">一行数据</param> /// <returns></returns> private static User_model ToModel(DataRow row) { User_model model = new User_model(); ///////////////////////////////////////////////////////// //还没写完,因为还没用到,所以以后再写 model.userId = (int)row["User_Id"]; model.userName = (string)row["User_Name"]; ///////////////////////////////////////////////////////// return(model); }
/// <summary> /// 判断登录是否成功,成功返回true,失败返回false /// 使用者:People控制器里的ajaxMakeLogin /// </summary> /// <param name="userName">用户名</param> /// <param name="password">密码</param> /// <returns></returns> public bool LoginYes(string userName, string password) { User_dal user_dal = new User_dal(); /*object AccountCount = user_dal.GetAccountCount(userName);//用户的数量 * //null代表数据库不存在该数据,System.DBNull.Value代表数据库里存在数据,但是该字段的值为null * if (AccountCount == null || AccountCount == System.DBNull.Value) * { * return false; * } * //如果用户的数量小于0 * if ((int)AccountCount <= 0) * { * return false; * } * if ((int)AccountCount > 1) * { * return false; * }*/ //以上判断存在该用户后,获取其盐值和密码 User_model user_model = new User_model(); user_model = user_dal.GetPwdAndSaltModel(userName); try { user_model = user_dal.GetPwdAndSaltModel(userName); } catch (Exception e) { //数据库异常处理,数据库里存在大于两条用户名一样的数据,抛出异常 throw new Exception(e.ToString()); } //finally { } string salt = user_model.salt; //颜值 string realPassword = user_model.password; //密码 //将盐值加在密码的后面,并转化为二进制 byte[] pwdAndSaltBytes = System.Text.Encoding.UTF8.GetBytes(password + salt); //经过哈希算法加密后得到的二进制值 byte[] hashBytes = new System.Security.Cryptography.SHA256Managed().ComputeHash(pwdAndSaltBytes); string hashPassword = Convert.ToBase64String(hashBytes); //判断密码是否正确 if (realPassword == hashPassword) { return(true); } else { return(false); } }
protected void Button1_Click(object sender, EventArgs e) { Config path = new Config(); User_model user_model = new User_model() { UserName = Textfullname.Text, Password = Textpassword.Text, Name = Textusername.Text, EmailId = Textemail.Text, UserType = DropDownList_usertype.SelectedValue }; User_model user_model_proto = new User_model() { Name = Textusername.Text }; dbget = Db4oFactory.OpenFile(path.addressOfUser); IObjectSet result = dbget.QueryByExample(user_model_proto); if (result.HasNext()) { flag = 0; Textusername.Text = ""; ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Username already present, Choose new one')", true); } dbget.Close(); if (flag == 1) { db = Db4oFactory.OpenFile(path.addressOfUser); db.Store(user_model); db.Close(); Textfullname.Text = ""; Textpassword.Text = ""; Textusername.Text = ""; Textemail.Text = ""; DropDownList_usertype.SelectedValue = ""; ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('User has been added sucessfully')", true); } }
//未编辑 /// <summary> /// 封装sqlparameters的功能 /// </summary> /// <param name="?"></param> /// <returns></returns> //public SqlParameter[] pp(params Dictionary<string,string>) //{ // SqlParameter[] ppp=new SqlParameter[]{ // }; // return ppp; //} /// <summary> /// 查询tb_User表,获取指定的一行数据 /// </summary> /// <param name="user_id"></param> /// <returns></returns> public User_model Get(string user_id) { DataTable dt = SqlHelper.ExecuteDataTable("select * from tb_User where User_Id=@user_id", new SqlParameter("@user_id", user_id)); if (dt.Rows.Count > 1) { throw new Exception("more than 1 row was found"); } if (dt.Rows.Count <= 0) { return(null); } ///////////////////////////////////////////////////////// //还没写完,因为还没用到,所以以后再写 DataRow row = dt.Rows[0]; User_model model = ToModel(row); ///////////////////////////////////////////////////////// return(model); }
protected void Button2_Click(object sender, EventArgs e) { Config path = new Config(); User_model user_model_proto = new User_model() { Name = Textusername.Text }; dbget = Db4oFactory.OpenFile(path.addressOfUser); IObjectSet result = dbget.QueryByExample(user_model_proto); if (result.HasNext()) { Textusername.Text = ""; ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Username already present, Choose new one')", true); } else { ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Username available')", true); } dbget.Close(); }