/// <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"); } }
/// <summary> /// 接受注册信息,并将用户信息写入数据库 /// </summary> /// <param name="ls">注册信息参数列表</param> /// <exception cref="MySqlException"></exception> /// <exception cref="Exception"></exception> /// <returns>用户 ID</returns> public int AccepteRegister(RegisterModel reg) { try { _ = GlobalFunc.CheckRegisterInput(reg); } catch (Exception ex) { throw ex; } if (reg != null) { var newReader = new ReaderInfo(); reg.Salt = Guid.NewGuid().ToString(); reg.PasswordHash = GlobalFunc.EncryptPassword(reg.Password, reg.Salt); newReader.SetInitial(reg); try { var id = DbContext.DBstatic.Insertable(newReader).ExecuteReturnIdentity(); Console.WriteLine("注册成功"); return(id); } catch (MySqlException ex) { throw ex; } catch (Exception ex) { throw ex; } } throw new Exception("Failed to register"); }
/// <summary> /// 创建管理员账户 /// </summary> /// <param name="ls"></param> /// <exception cref="MySqlException"></exception> /// <exception cref="Exception"></exception> /// <returns>用户 ID</returns> public int CreateAdminAccount(RegisterModel reg) { int newAdId; try { _ = GlobalFunc.CheckRegisterInput(reg); } catch (Exception ex) { throw ex; } if (reg != null) { var newAdmin = new AdminInfo(); reg.Salt = Guid.NewGuid().ToString(); reg.PasswordHash = GlobalFunc.EncryptPassword(reg.Password, reg.Salt); newAdmin.SetInitial(reg); try { newAdId = DbContext.DBstatic.Insertable(newAdmin).ExecuteReturnIdentity(); } catch (MySqlException ex) { throw ex; } } else { throw new Exception("Failed to create account"); } return(newAdId); }