Example #1
0
        /// <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);
            }
        }