private void button_User_Click(object sender, EventArgs e) { User person = new User(int.Parse(uid), uac, ucl, upw); Form_User user = new Form_User(person); user.ReturnUser += (o, e1) => { if (!e1.CanUpdate) { return; } User now = e1.Me; // 更新资料 string question = "Change " + now.GetRawString(), answer; TcpClientP request = new TcpClientP(); request.Connect(serverIP, Port.TCP_LOGIN_PORT); request.Query(question, out answer); if (answer != VerMessage.CHANGE_SUCCESS) { MessageBox.Show("更新信息失败", "提示", MessageBoxButtons.OK); request.Close(); return; } MessageBox.Show("更新信息成功", "提示", MessageBoxButtons.OK); request.Close(); ucl = now.name; Registry.AddKey2Registry("PublishClient", "CurrentUserName", ucl); this.Text = "教材补助经费评估软件 [" + ucl + "]" + " [#" + uid + "]"; this.label_ClientName.Text = "[#" + uid + "] " + ucl; }; user.ShowDialog(); }
/// <summary> /// 用户登录操作 /// </summary> /// <param name="sender">基类</param> /// <param name="e">环境变量</param> private void button_Login_Click(object sender, EventArgs e) { var uac = textBox_UesrAccount.Text; var upw = textBox_UserPW.Text; textBox_UserPW.Text = ""; // 空输入 if (uac.Length == 0) { MessageBox.Show("账户不能为空!", "输入错误", MessageBoxButtons.OK); return; } if (upw.Length == 0) { MessageBox.Show("密码不能为空!", "输入错误", MessageBoxButtons.OK); return; } if (uac.Contains(' ') || upw.Contains(' ')) { MessageBox.Show("任何字段不得包含空格与回车字符!", "输入错误", MessageBoxButtons.OK); return; } User client; int idx = users.Find(uac, out client); // 用户不存在 if (idx == -1) { string msg = "用户[" + uac + "]不存在!"; MessageBox.Show(msg, "用户错误", MessageBoxButtons.OK); return; } // 检查密码 upw = Cipher.md5Encrypt(upw); if (!client.testPassword(upw)) { string msg = "用户[" + uac + "]密码错误!"; MessageBox.Show(msg, "密码错误", MessageBoxButtons.OK); return; } // 检查权限 if (!client.testAdmin()) { string msg = "用户[" + uac + "]不是管理员!登录失败。"; MessageBox.Show(msg, "权限错误", MessageBoxButtons.OK); return; } else { string msg = "用户[" + uac + "]是管理员!登录成功。正在初始化数据,请稍等。"; MessageBox.Show(msg, "登陆成功", MessageBoxButtons.OK); Registry.AddKey2Registry("PublishServer", "CurrentUserID", client.userID.ToString()); Registry.AddKey2Registry("PublishServer", "CurrentUserAccount", uac); Registry.AddKey2Registry("PublishServer", "CurrentUserName", client.name); this.DialogResult = DialogResult.OK; this.Close(); } }
/// <summary> /// 用户登录操作 /// </summary> /// <param name="sender">基类</param> /// <param name="e">环境变量</param> private void button_Login_Click(object sender, EventArgs e) { var uac = textBox_UesrAccount.Text; var upw = textBox_UserPW.Text; textBox_UserPW.Text = ""; // 空输入 if (uac.Length == 0) { MessageBox.Show("账户不能为空!", "输入错误", MessageBoxButtons.OK); return; } if (upw.Length == 0) { MessageBox.Show("密码不能为空!", "输入错误", MessageBoxButtons.OK); return; } if (uac.Contains(' ') || upw.Contains(' ')) { MessageBox.Show("任何字段不得包含空格与回车字符!", "输入错误", MessageBoxButtons.OK); return; } upw = Cipher.md5Encrypt(upw); string question = "Login " + uac + " " + upw; string ret; tcpClientLogin.Query(question, out ret); // 用户不存在 if (ret == VerMessage.LOGIN_FAILED_NO_SUCH_USER) { string msg = "用户[" + uac + "]不存在!"; MessageBox.Show(msg, "用户错误", MessageBoxButtons.OK); return; } // 检查密码 if (ret == VerMessage.LOGIN_FAILED_WRONG_PW) { string msg = "用户[" + uac + "]密码错误!"; MessageBox.Show(msg, "密码错误", MessageBoxButtons.OK); return; } // 成功登陆 if (ret == VerMessage.LOGIN_SUCCESS) { string msg = "用户[" + uac + "]登录成功。正在初始化数据,请稍等。"; MessageBox.Show(msg, "登陆成功", MessageBoxButtons.OK); Registry.AddKey2Registry("PublishClient", "CurrentUserPW", upw); isLogin = true; while (!isCatch) { Thread.Sleep(100); } this.DialogResult = DialogResult.OK; tcpClientLogin.Close(); this.Close(); } }
/// <summary> /// 关闭窗口时保存数据 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Form_Login_FormClosing(object sender, System.Windows.Forms.FormClosingEventArgs e) { tcpClientLogin.Close(); tcpListenerUser.Stop(); if (this.textBox_UesrAccount.Text == null) { return; } string lastUser = this.textBox_UesrAccount.Text; Registry.AddKey2Registry("PublishClient", "LastUser", lastUser); }
/// <summary> /// 存储新的用户登录信息 /// </summary> /// <param name="path">文件路径</param> private void SaveUsersData(string path) { FileStream fileStream = new FileStream(path, FileMode.Create); byte[] serBytes; ToBytes <UserSet> .GetBytes(ref users, out serBytes); string longkey = Cipher.getInitVector(24); string key = longkey.Substring(0, 16); string iv = longkey.Substring(16, 8); byte[] cipher = Cipher.AESEncrypt(serBytes, key, iv); fileStream.Write(cipher, 0, cipher.Length); fileStream.Flush(); fileStream.Close(); fileStream.Dispose(); Registry.AddKey2Registry("PublishServer\\Encrypt", "SecretKey", key); Registry.AddKey2Registry("PublishServer\\Encrypt", "InitVector", iv); }
public void OnRecvData(object sender, EventArgs e) { TcpClient tcpClient = sender as TcpClient; int threadID = Port.TCP_USER_FILE_PORT % 10000; using (NetworkStreamP buf = new NetworkStreamP(tcpClient.GetStream())) { buf.ReceiveBufferSize = tcpClient.ReceiveBufferSize; while (true) { try { IPEndPoint where = tcpClient.Client.RemoteEndPoint as IPEndPoint; IPAddress clientIP = where.Address; string question; buf.Read(out question); string[] result = question.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); Registry.AddKey2Registry("PublishClient", "CurrentUserID", result[0]); Registry.AddKey2Registry("PublishClient", "CurrentUserAccount", result[1]); Registry.AddKey2Registry("PublishClient", "CurrentUserName", result[2]); isCatch = true; } catch (Exception ex) { Type type = ex.GetType(); if (type == typeof(TimeoutException)) { // 超时异常,不中断连接 Console.WriteLine("{0} [client {1}]: 数据超时失败!", DateTime.Now, threadID); } else { // 仍旧抛出异常,中断连接 Console.WriteLine("{0} [client {1}]: 中断连接异常原因:{2}!", DateTime.Now, threadID, type.Name); throw ex; } } } } }
private void button_User_Click(object sender, EventArgs e) { User person; string uac = Registry.ReadKey4Registry("PublishServer", "CurrentUserAccount"); users.Find(uac, out person); Form_User user = new Form_User(person); user.ReturnUser += (o, e1) => { if (!e1.CanUpdate) { return; } User now = e1.Me; users.ReplaceTo(uac, now); SaveUsersData(rtUserPath); ucl = now.name; Registry.AddKey2Registry("PublishServer", "CurrentUserName", ucl); this.Text = "教材补助经费评估软件 [" + ucl + "]" + " [#" + uid + "]"; this.label_AdminName.Text = ucl; }; user.ShowDialog(); }
private void button_Launch_Click(object sender, EventArgs e) { Registry.AddKey2Registry("PublishClient", "ServerIP", serverIP.ToString()); this.DialogResult = DialogResult.OK; }