客户端登陆界面如下:

录播教室预约系统(五)-用户登陆_客户端

客户端代码:

录播教室预约系统(五)-用户登陆_教室预约_02

                   Program.cs 中


                   NetworkComms.IgnoreUnknownPacketTypes = =  ConnectionInfo(, =
                    MainForm mainForm = 
                    frmLogin loginForm = 
                     (loginForm.ShowDialog() ==(loginForm.Register == =

录播教室预约系统(五)-用户登陆_教室预约_02

登陆窗口中的登陆按钮相关代码

录播教室预约系统(五)-用户登陆_教室预约_02

 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);
                }
            }
        }

录播教室预约系统(五)-用户登陆_教室预约_02

User.cs实体类代码:

录播教室预约系统(五)-用户登陆_教室预约_06 Users实体类,支持Protobuf.net序列化

服务器端程序的处理代码:

 

在服务器端构造函数中声明针对登陆操作的处理器:

    //用户验证处理器
            NetworkComms.AppendGlobalIncomingPacketHandler<Users>("Login", HandleUserVerify);

录播教室预约系统(五)-用户登陆_教室预约_02

 //用户登录验证
        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);

          

        }

录播教室预约系统(五)-用户登陆_教室预约_02

录播教室预约系统(五)-用户登陆_客户端_09 LoginResult契约类

 

DoUsers类中相关方法

录播教室预约系统(五)-用户登陆_教室预约_02

   public static Users GetUserByID(string userID)
        {            using (IDataReader reader = DBUsers.GetOneByUserID(userID))
            {
                
                Users theUser = PopulateFromReader(reader);                return theUser;

             
            }

        }

录播教室预约系统(五)-用户登陆_教室预约_02

录播教室预约系统(五)-用户登陆_教室预约_02

   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;
        }

录播教室预约系统(五)-用户登陆_教室预约_02

 

DBUsers类中相关方法

录播教室预约系统(五)-用户登陆_教室预约_02

  //根据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();

        }

录播教室预约系统(五)-用户登陆_教室预约_02

数据库中相关的存储过程

录播教室预约系统(五)-用户登陆_教室预约_02

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

录播教室预约系统(五)-用户登陆_教室预约_02