private string AddUser(string content) { if (string.IsNullOrWhiteSpace(content)) return Consts.MAN_ADD_USER_ERR + "无用户信息."; content = content.Trim(); string[] sa = content.Split(new string[] { "\t" }, StringSplitOptions.RemoveEmptyEntries); if (sa == null || sa.Length != Consts.USER_ACCOUNT_ITEM_COUNT) return Consts.MAN_ADD_USER_ERR + "非法用户信息 :" + content; string userName = sa[0].Trim(); string password = sa[1].Trim(); string permission = sa[2].Trim(); if (Helper.CheckValidChar(userName, Helper.CheckMethod.CharNum) == false) return Consts.MAN_ADD_USER_ERR + "无效用户名 : " + userName; if (Helper.CheckValidChar(password, Helper.CheckMethod.CharNum) == false) return Consts.MAN_ADD_USER_ERR + "无效密码 :" + password; if (permission != "1" && permission != "2") return Consts.MAN_ADD_USER_ERR + "无效权限 : " + permission; lock (_taskLock) { UserInfo ui = Helper.FindUserInfo(userName, _userInfoOc); if (ui != null) return Consts.MAN_ADD_USER_ERR + "重复用户 : " + userName; UserInfo uiNew = new UserInfo() { DisplayIcon = false, UserName = userName, Password = password, Permission = permission }; if(permission == "2") uiNew.OnlineOfflineEvent += new UserInfo.OnlineOfflineEventHandler(UserInfo_OnlineOfflineEvent); _userInfoOc.Add(uiNew); SaveUser(); } return Consts.MAN_ADD_USER_OK; }
private void LoadUser() { _userInfoOc.Clear(); StreamReader sr = null; try { string folder = System.Environment.GetFolderPath(Environment.SpecialFolder.CommonDocuments); sr = new StreamReader(folder + @"\COMWAY\Service\config\ituser.dat"); string line = null; while (true) { line = sr.ReadLine(); if (string.IsNullOrWhiteSpace(line)) break; string[] sa = line.Split(new string[] { "\t" }, StringSplitOptions.None); if (sa == null || sa.Length != Consts.USER_ACCOUNT_ITEM_COUNT) { eventLogInformationTransfer.WriteEntry("用户信息错误 : " + line, EventLogEntryType.Warning); continue; } string user = sa[0].Trim(); string pw = sa[1].Trim(); string pm = sa[2].Trim(); string ruser = EncryptDecrypt.Decrypt(user); string rpw = EncryptDecrypt.Decrypt(pw); string rpm = EncryptDecrypt.Decrypt(pm); if (rpm != "1" && rpm != "2") { eventLogInformationTransfer.WriteEntry("用户权限错误 : " + ruser, EventLogEntryType.Warning); rpm = "2"; } UserInfo ui = Helper.FindUserInfo(ruser, _userInfoOc); if (ui != null) eventLogInformationTransfer.WriteEntry("重复用户 : " + ruser, EventLogEntryType.Warning); else { UserInfo uiNew = new UserInfo() { DisplayIcon = false, UserName = ruser, Password = rpw, Permission = rpm }; if (rpm == "2") uiNew.OnlineOfflineEvent +=new UserInfo.OnlineOfflineEventHandler(UserInfo_OnlineOfflineEvent); _userInfoOc.Add(uiNew); } } sr.Close(); sr.Dispose(); } catch (Exception ex) { Helper.SafeCloseIOStream(sr); eventLogInformationTransfer.WriteEntry("装载用户信息出现错误 : " + ex.Message + "\n\n" + ex.StackTrace, EventLogEntryType.Warning); } lock (_taskLock) { UserInfo ui = Helper.FindUserInfo(Consts.LOGIN_ADMIN_USERNAME, _userInfoOc); if (ui != null) _userInfoOc.Remove(ui); _userInfoOc.Add(new UserInfo() { DisplayIcon = false, UserName = Consts.LOGIN_ADMIN_USERNAME, Password = Consts.LOGIN_ADMIN_PASSWORD, Permission = Consts.LOGIN_ADMIN_PERMISSION }); } }