Beispiel #1
0
        /// <summary>
        ///     修改密码
        /// </summary>
        /// <param name="id">读者 ID</param>
        /// <param name="oldPw">原密码</param>
        /// <param name="newPw">新密码</param>
        public void ReaderPasswordChange(int id, string newPw, string oldPw = null)
        {
            if (!GlobalFunc.IsValidPassword(newPw))
            {
                throw new Exception("Password Invalid");
            }
            if (DbContext.DBstatic.Queryable <ReaderInfo>().InSingle(id) == null)
            {
                throw new Exception("Invalid ID");
            }
            if (oldPw == null || GlobalFunc.VerifyPassword(id, oldPw))
            {
                var salt    = Guid.NewGuid().ToString();
                var pwHash  = GlobalFunc.EncryptPassword(newPw, salt);
                var newInfo = new RegisterModel
                {
                    ID           = id,
                    PasswordHash = pwHash,
                    Salt         = salt
                };
                ReaderInfoModel tmpDic;
                try
                {
                    tmpDic = GetReaderInfo(id);
                }
                catch (MySqlException ex)
                {
                    throw ex;
                }
                catch (Exception ex)
                {
                    throw ex;
                }

                newInfo.UpdateInfo(tmpDic);
                var readerPw = new ReaderInfo().UpdatePassword(newInfo);
                try
                {
                    DbContext.DBstatic.Updateable(readerPw).IgnoreColumns(true, ignoreAllDefaultValue: true)
                    .ExecuteCommand();
                }
                catch (MySqlException ex)
                {
                    throw ex;
                }
                catch (Exception ex)
                {
                    throw ex;
                }

                if (!GlobalFunc.VerifyPassword(id, newPw))
                {
                    throw new Exception("Failed Change");
                }
            }
            else
            {
                throw new Exception("Wrong Password");
            }
        }