public bool AuthenticateUser(EnvyUser user)
        {
            while (!terminate)
            {
                var messages = ConsumeNetMessages();
                foreach (var msg in messages)
                {
                    if (msg == null)
                    {
                        continue;
                    }
                    switch (msg.Type)
                    {
                    case EnvyNetMessage.Types.NetMessageType.NetOpenConnection:
                    {
                        activeConnection.PushNetworkStream(Encoding.ASCII.GetBytes("ACCEPT\n"));
                        break;
                    }

                    case EnvyNetMessage.Types.NetMessageType.NetAuthMessage:
                    {
                        var response = user.username + ":" + user.password;
                        activeConnection.PushNetworkStream(Encoding.ASCII.GetBytes(response + "\n"));
                        break;
                    }

                    case EnvyNetMessage.Types.NetMessageType.NetAuthResponse:
                    {
                        if (msg.DataSize == -1)
                        {
                            Console.WriteLine("INVALID LOGIN");
                            return(false);
                        }
                        else
                        {
                            String response = msg.Data.ToStringUtf8();
                            var    info     = response.Split(':');
                            Console.WriteLine("Welcome: " + info[0] + " you are an " + info[1]);
                            return(true);
                        }
                        break;
                    }

                    case EnvyNetMessage.Types.NetMessageType.NetCloseConnection:
                    {
                        terminate = true;
                        break;
                    }
                    }
                }
            }
            return(false);
        }
Beispiel #2
0
        private void btnLogin_Click(object sender, EventArgs e)
        {
            String         username = tbUsr.Text;
            String         password = tbPswd.Text;
            EnvyUser       user     = new EnvyUser(username, password);
            String         host     = tbHost.Text;
            EnvyConnection conn     = new EnvyConnection(host, 2356);

            if (conn.Connect())
            {
                Console.WriteLine("CONNECTED");
                var ap = new EnvyClientAuthProtocol(conn);
                if (ap.AuthenticateUser(user))
                {
                    lblStatus.Visible = true;
                }
                ap.AuthenticateUser(user);
            }
        }