Click here to Skip to main content
15,905,781 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, i'm making a login feature using tcpclient/tcplistner but i'm only able to send the username input to the server through the stream and not the password input. The string that the server recieve now is like "usernamepassword". Is there a way to send both inputs username and password as two diffrent variables? i've tried to separate the username:password string with the Split(':') and then displaying the output on the server like this:
C#
while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
     {
          data = Encoding.ASCII.GetString(bytes, 0, i);

          string[] credentials = data.Split(':');

          string username = data[0].ToString();
          string password = data[1].ToString();

          Console.WriteLine("Username: {0}", username);
          Console.WriteLine("Password: {0}", password);
      }


but the output is only the first character of the username and the password for example if the credential string is username:password the output will be:

Username: u
Password: p


Why is it like this? and the best way would be to send two strings instead of splitting it but couldn't get that to work ethier.

Any help would be appreciated.
Posted

C#
while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
     {
          data = Encoding.ASCII.GetString(bytes, 0, i);
 
          string[] credentials = data.Split(':');
 
          string username = credentials[0];
          string password = credentials[1];
 
          Console.WriteLine("Username: {0}", username);
          Console.WriteLine("Password: {0}", password);
      }


That would be the way to do it, use credentials, not data, otherwise you are just taking the first and second letter of the data.
 
Share this answer
 
Comments
maxbre 12-Jun-13 15:20pm    
Oh yes ofcourse! i'm stupid.. Thanks alot! any idea if I could just send the two inputs as two diffrent variables? instead of sending one string and split it?
Ron Beyer 12-Jun-13 15:40pm    
You could send two messages with some kind of identifier, or you can use the BinaryWriter to write strings into the data then BinaryReader to read them back out, but why? This method works fine, other than sending passwords plain-text.
maxbre 12-Jun-13 15:53pm    
Ok thanks. I will encrypt the passwords later.
maxbre wrote:
Ok thanks. I will encrypt the passwords later.
Important! "Encrypting password" is not a right idea.

Please see my past answers:
i already encrypt my password but when i log in it gives me an error. how can decrypte it[^],
TCP Connection with username and password[^],
Decryption of Encrypted Password[^],
storing password value int sql server with secure way[^].

Ideally, you should use it with HTTPS, because the ceremony of creation of the password can itself be eavesdropped from the very beginning. In such case, the malicious artist won't need your password but would used the hash as is. Please see:
http://en.wikipedia.org/wiki/HTTPS[^],
http://en.wikipedia.org/wiki/Transport_Layer_Security[^].

—SA
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900