您好,登錄后才能下訂單哦!
客戶端登陸界面如下:
客戶端代碼:
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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。