Ejemplo n.º 1
0
        public Respone Login(LoginRequest request)
        {
            var res = DBServer.Instance.GetPassword(request.Account);

            if (res.IsSuccess)
            {
                var    oldPass  = res.Data.Rows[0]["password"].ToString();
                var    salt     = Convert.FromBase64String(res.Data.Rows[0]["salt"].ToString());
                var    saltpass = Encoding.UTF8.GetBytes(request.Password).Concat(salt).ToArray();
                MD5Cng md5      = new MD5Cng();
                var    crpPass  = Convert.ToBase64String(md5.ComputeHash(saltpass));

                if (oldPass == crpPass)
                {
                    Context.Login(request.Account);
                    return(StandResult(StandRespone.SuccessResult("登录成功")));
                }
                else
                {
                    return(StandResult(StandRespone.FailResult("登录失败,密码错误")));
                }
            }
            else
            {
                return(StandResult(res));
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 用户注册
        /// </summary>
        /// <param name="userAccount">用户账号</param>
        /// <param name="password">密码</param>
        /// <param name="salt">盐</param>
        /// <example>
        ///     <code lang="C#">
        ///         //加密密码
        ///         byte[] salt = new byte[20];
        ///         new Random().NextBytes(salt);
        ///         MD5Cng md5 = new MD5Cng();
        ///         var saltpass = Encoding.UTF8.GetBytes(password).Concat(salt).ToArray();
        ///         var crpPass = md5.ComputeHash(saltpass);
        ///     </code>
        /// </example>

        public StandRespone Regeister(string userAccount, string password, string salt)
        {
            if (!userAccount.IsDBSafe() || !password.IsDBSafe())
            {
                return(StandRespone.UnSafeResult());
            }

            var executer = DBExecuterFactory.CreateDBExecuter();

            try
            {
                var queryExitUser = $"select* from userinfo where account = '{userAccount}'";
                if (executer.ExecuteNonQuery(queryExitUser) > 0)
                {
                    return(new StandRespone(false, "用户已存在!"));
                }

                var trans      = executer.Connection.BeginTransaction();
                var insertUser = $"insert into userinfo (account,create_date,status) " +
                                 $"values('{userAccount}',now(),'{UserStatus.Normal}');" +
                                 $"insert into user_password(user_id,password,salt) " +
                                 $"values(@@identity,'{password}','{salt}')";
                executer.ExecuteNonQuery(insertUser);
                trans.Commit();

                return(StandRespone.SuccessResult("注册成功"));
            }
            catch (Exception e)
            {
                executer.Close();
                return(StandRespone.FailResult("发生异常:" + e.Message));
            }
            finally
            {
                executer.Close();
            }
        }