客户端登陆界面如下:
客户端代码:
Program.cs 中 NetworkComms.IgnoreUnknownPacketTypes = = ConnectionInfo(, = MainForm mainForm = frmLogin loginForm = (loginForm.ShowDialog() ==(loginForm.Register == =
登陆窗口中的登陆按钮相关代码
private void btnLogin_Click(object sender, EventArgs e) { // 如果通过输入验证 if (UserInputCheck()) { try { // 保存登录身份是否合法验证结果 bool isPass = false; Users theUser = new Users(); theUser.UserID = txtLoginName.Text.Trim(); theUser.Password = txtUserPwd.Text.Trim(); //发送当前登陆用户的信息给服务器,并从服务器获取返回的信息 LoginResult loginResult = newTcpConnection.SendReceiveObject<LoginResult>("Login", "LoginResult", 5000, theUser); if (loginResult.Message=="登录成功") { //记录用户名 netConfiguration.UserID = theUser.UserID; netConfiguration.Save(); //初始化主窗口中的属性 theUser.Department = loginResult.Department; theUser.Name = loginResult.UserName; mainForm.InitiaLize(this.newTcpConnection, theUser); // 标识验证通过 isPass = true; Register = 5; this.DialogResult = DialogResult.OK; } // 如果未通过验证 if (!isPass) { MessageBox.Show(loginResult.Message); } } catch (Exception ex) { ShowLoginLostToolTip(ex); } } }
User.cs实体类代码:
Users实体类,支持Protobuf.net序列化
服务器端程序的处理代码:
在服务器端构造函数中声明针对登陆操作的处理器:
//用户验证处理器 NetworkComms.AppendGlobalIncomingPacketHandler<Users>("Login", HandleUserVerify);
//用户登录验证 private void HandleUserVerify(PacketHeader header, Connection connection, Users theUser) { LoginResult loginResult = new LoginResult(); Users currentUser = DoUsers.GetUserByID(theUser.UserID); string message; if (currentUser != null) { if (currentUser.Password == theUser.Password) message = "登录成功"; else if (currentUser.Password == "msdcmsdcmsdcmsdcmsdc") message = "用户未开通,请与管理员联系"; else if (currentUser.Password != theUser.Password) message = "用户名密码不匹配"; else message = "登录不成功,原因未知"; } else { message = "用户不存在"; } loginResult.Message = message; loginResult.Department = currentUser.Department; loginResult.UserName = currentUser.Name; connection.SendObject("LoginResult", loginResult); }
LoginResult契约类
DoUsers类中相关方法
public static Users GetUserByID(string userID) { using (IDataReader reader = DBUsers.GetOneByUserID(userID)) { Users theUser = PopulateFromReader(reader); return theUser; } }
private static Users PopulateFromReader(IDataReader reader) { Users Users = new Users(); if (reader.Read()) { Users.Id = Convert.ToInt32(reader["Id"]); Users.UserID = reader["UserID"].ToString(); Users.Name = reader["Name"].ToString(); Users.Password = reader["Password"].ToString(); Users.Declaring = reader["Declaring"].ToString(); Users.Status = Convert.ToInt32(reader["Status"]); Users.IsMale = Convert.ToBoolean(reader["IsMale"]); Users.UserLevel = Convert.ToInt32(reader["UserLevel"]); Users.Enabled = Convert.ToBoolean(reader["Enabled"]); Users.RegisterTime = Convert.ToDateTime(reader["RegisterTime"]); Users.LastLoginTime = Convert.ToDateTime(reader["LastLoginTime"]); Users.DepID = Convert.ToInt32(reader["DepID"]); Users.Department = reader["Department"].ToString(); } return Users; }
DBUsers类中相关方法
//根据UserID获取记录 public static IDataReader GetOneByUserID(string userID) { SqlParameterHelper sph = new SqlParameterHelper(GetReadConnectionString(), "Users_SelectOneByUserID", 1); sph.DefineSqlParameter("@UserID", SqlDbType.NVarChar, 200, ParameterDirection.Input, userID); return sph.ExecuteReader(); }
数据库中相关的存储过程
CREATE PROCEDURE [dbo].Users_SelectOneByUserID @UserID nvarchar(100) AS SELECT [Id], [UserID], [Name], [Password], [Declaring], [Status], [IsMale], [UserLevel], [Enabled], [RegisterTime], [LastLoginTime], [DepID], [Department] FROM [dbo].[Users] WHERE UserID=@UserID
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。