Пример #1
0
        static string Logowanie()
        {
            ////////////////////////////////////////
            Console.WriteLine("Zaloguj się podając login i hasło:");
            var login = Console.ReadLine();
            var haslo = Console.ReadLine();

            Name = login;

            var data = new StringBuilder(login);

            data.Append(" ");
            data.Append(haslo);
            /////////////////////////////////////////

            var       IP        = IPAddress.Parse("127.0.0.1");
            TcpClient tcpClient = new TcpClient(new IPEndPoint(IP, 0));

            tcpClient.Connect(IPAddress.Parse("127.0.0.1"), 8086);
            SslStream sslClient = new SslStream(tcpClient.GetStream(), false, new RemoteCertificateValidationCallback(ValidateServerCertificate), null);

            try
            {
                sslClient.AuthenticateAsClient("TIPserver");
                //Console.WriteLine("Uwierzytelniono");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            ULP cmd = new ULP(ulpOperation.LOGIN, data.ToString());
            //Console.WriteLine("Wysłano: " + cmd.ToString());

            string msg = cmd.ToString();

            sslClient.Write(ASCIIEncoding.ASCII.GetBytes(msg), 0, msg.Length);
            byte[] tab    = new byte[150];
            int    i      = sslClient.Read(tab, 0, 150);
            ULP    result = new ULP(Encoding.ASCII.GetString(tab));

            Console.WriteLine(result.OperationStatus.ToString());
            //Console.WriteLine(Encoding.ASCII.GetString(tab),0,i);
            //Console.WriteLine("Odebrano: " + result.ToString());
            //Console.WriteLine("");
            sslClient.Close();
            tcpClient.Close();
            return(result.data);
        }
Пример #2
0
        /*<summary>
         * sends login and password for login/register and return response from server
         * </summary>
         */
        private ULP send_login_data(ulpOperation operation, Int32 port)
        {
            machineName = txt_server_add.Text;
            TcpClient client = new TcpClient(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 0));

            client.Connect(server_addres, 8086);
            SslStream sslStream = new SslStream(
                client.GetStream(),
                false,
                new RemoteCertificateValidationCallback(ValidateServerCertificate),
                null
                );

            try
            {
                sslStream.AuthenticateAsClient(serverName);
            }
            catch (AuthenticationException e)
            {
                Console.WriteLine("Exception: {0}", e.Message);
                if (e.InnerException != null)
                {
                    Console.WriteLine("Inner exception: {0}", e.InnerException.Message);
                }
                Console.WriteLine("Authentication failed - closing the connection.");
                client.Close();
            }

            ULP sendFrame = new ULP(operation, txt_login.Text + ' ' + txt_pass.Text);

            byte[] sendBytes = Encoding.ASCII.GetBytes(sendFrame.ToString());
            sslStream.Write(sendBytes);
            sslStream.Flush();
            //client.GetStream().WriteAsync(sendBytes, 0, sendBytes.Length);
            byte[] buffer = new byte[1500];
            sslStream.Read(buffer, 0, buffer.Length);
            // Close the client connection.
            client.Close();
            ULP frame = new ULP(Encoding.ASCII.GetString(buffer, 0, buffer.Length));

            return(frame);
        }
Пример #3
0
        private void btn_Sign_in_Click(object sender, EventArgs e)
        {
            if (txt_login.Text == "" || txt_pass.Text == "")
            {
                MessageBox.Show("no login or/and password");
                return;
            }
            else
            {
                ULP frame = send_login_data(ulpOperation.LOGIN, _login_port);
                switch (frame.OperationStatus)
                {
                case ulpOperStatus.SUCCESS:
                {
                    var token = frame.data;
                    MessageBox.Show(token);
                    Hide();
                    Main_form fm = new Main_form(token, txt_login.Text);
                    fm.Show();
                }
                break;

                case ulpOperStatus.NONE:
                {
                    MessageBox.Show("Something wrong, please try again");
                }
                break;

                case ulpOperStatus.WRONG_LOGIN:
                {
                    MessageBox.Show("Wrong login/or user is logged, please try again");
                }
                break;

                case ulpOperStatus.WRONG_PASS:
                {
                    MessageBox.Show("Wrong password, please try again");
                }
                break;
                }
            }
        }