Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

C# Sockets | Authenticating not working

Discussion in 'Scripting' started by GoldenretriverYT, Aug 2, 2019.

  1. GoldenretriverYT

    GoldenretriverYT

    Joined:
    May 8, 2019
    Posts:
    13
    Hello, I try to make a login in unity for my multiplayer game. The problem is, when I send the username and the password to the Server, its send to the api from the game. But the API is giving "INVALID_CRED".
    Now, when I try to use the url that is debugged in the console (NO difference to the used url in the http get request) it works in the web browser with no problems (I get my AuthToken), and if I simulate it (I build it in as feature of the ClientManager), it is working too. Every censored password in the image is the same.

    Heres my code:

    SERVER (unity independent):

    Code (CSharp):
    1. using System;
    2.     using System.Net;
    3.     using System.Net.Sockets;
    4.     using System.Threading.Tasks;
    5.     using System.IO;
    6.     using System.Text;
    7.     using System.Diagnostics;
    8.  
    9.     public class ClientManager
    10.     {
    11.         private static TcpListener listener;
    12.  
    13.         public static void Main()
    14.         {
    15.             listener = new TcpListener(IPAddress.Any, 10250);
    16.             listener.Start();
    17.             Console.WriteLine("*** ClientManager started ***");
    18.             Console.WriteLine("Listening to port 10250, make sure not used.");
    19.             StartAccept();
    20.  
    21.             while (true)
    22.             {
    23.                 System.Threading.Thread.Sleep(1000);
    24.                 string cmd = Console.ReadLine();
    25.  
    26.                 if (cmd.Equals("exit"))
    27.                 {
    28.                     Console.WriteLine("*** Stopping cman... ***");
    29.                     Environment.Exit(0);
    30.                 }
    31.  
    32.                 if (cmd.StartsWith("auth"))
    33.                 {
    34.  
    35.                     Console.WriteLine("[" + "simulate" + "] Using auth cred to authenticate.");
    36.                     char c = '|';
    37.                     string[] args = cmd.Split(c);
    38.  
    39.                     if (!(args.Length > 2))
    40.                     {
    41.                         Console.WriteLine("INVALID_DATA_GIVEN");
    42.                     }
    43.                     string username = args[1];
    44.                     string password = args[2];
    45.  
    46.                     Console.WriteLine("[" + "simulate" + "] Using credentials: " + username + " and " + password);
    47.  
    48.                     string response = GetAsync(("https://goldenarmy.eu/studios/id/cman/clogin.php?name=" + username + "&password=" + password + "&test=1"));
    49.                     Console.WriteLine("https://goldenarmy.eu/studios/id/cman/clogin.php?name=" + username + "&password=" + password);
    50.                     Console.WriteLine(response);
    51.  
    52.                     if (response.Equals("ERROR_FAILED_CONNECTION"))
    53.                     {
    54.                         Console.WriteLine("ERROR_SERVERERROR");
    55.                     }
    56.                     else if (response.Equals("INVALID_USERNAME"))
    57.                     {
    58.                         Console.WriteLine("INVALID_DATA_GIVEN");
    59.                     }
    60.                     else if (response.Equals("INVALID_PASSWORD"))
    61.                     {
    62.                         Console.WriteLine("INVALID_DATA_GIVEN");
    63.                     }
    64.                     else if (response.Equals("INVALID_CRED"))
    65.                     {
    66.                         Console.WriteLine("ERROR_AUTH_INVALID_CRED");
    67.                     }
    68.                     else if (response.Equals("IS_BANNED"))
    69.                     {
    70.                         Console.WriteLine("ERROR_AUTH_BANNED");
    71.                     }
    72.                     else
    73.                     {
    74.                         Console.WriteLine("[" + "simulate" + "] LOGIN OK | RESPONSE: " + response);
    75.                     }
    76.                 }
    77.             }
    78.  
    79.         }
    80.         private static void StartAccept()
    81.         {
    82.             listener.BeginAcceptTcpClient(HandleAsyncConnection, listener);
    83.         }
    84.         private static void HandleAsyncConnection(IAsyncResult res)
    85.         {
    86.             StartAccept();
    87.             TcpClient client = listener.EndAcceptTcpClient(res);
    88.             string clientSession = "NULL";
    89.             string ip = ((IPEndPoint)client.Client.RemoteEndPoint).Address.ToString();
    90.  
    91.             Console.WriteLine("[" + ip + "] Incoming connection.");
    92.  
    93.             while (true)
    94.             {
    95.                 try
    96.                 {
    97.                     System.Threading.Thread.Sleep(1000);
    98.                     Console.WriteLine("Trying to read data from " + ip);
    99.                     NetworkStream stream = null;
    100.                     Byte[] data = new Byte[1024];
    101.                     String responseData = String.Empty;
    102.                     Int32 bytes = 0;
    103.  
    104.                     stream = client.GetStream();
    105.                     bytes = stream.Read(data, 0, data.Length);
    106.                     responseData = System.Text.Encoding.ASCII.GetString(data);
    107.  
    108.  
    109.                     Console.WriteLine("[" + ip + "] " + responseData);
    110.  
    111.                     if (responseData.StartsWith("close"))
    112.                     {
    113.                         Console.WriteLine("[" + ip + "] Connection closed.");
    114.                         break;
    115.                     }else if (responseData.StartsWith("useauthtoken"))
    116.                     {
    117.                         Console.WriteLine("[" + ip + "] Using auth token to authenticate.");
    118.                         char c = '|';
    119.                         string[] args = responseData.Split(c);
    120.  
    121.                         if (!(args.Length > 1))
    122.                         {
    123.                             SendMessage(stream, "INVALID_DATA_GIVEN");
    124.                         }
    125.  
    126.                         string response = GetAsync("https://goldenarmy.eu/studios/id/cman/cauthtokencheck.php?auth=" + args[1]);
    127.  
    128.                         if (response.Equals("yes"))
    129.                         {
    130.                             clientSession = args[1];
    131.                             SendMessage(stream, "OK");
    132.                         }
    133.                         else
    134.                         {
    135.                             SendMessage(stream, "ERROR_AUTH_INVALID");
    136.                         }
    137.                     }
    138.                     else if (responseData.StartsWith("auth"))
    139.                     {
    140.  
    141.                         Console.WriteLine("[" + ip + "] Using auth cred to authenticate.");
    142.                         char c = '|';
    143.                         string[] args = responseData.Split(c);
    144.  
    145.                         if (!(args.Length > 2))
    146.                         {
    147.                             SendMessage(stream, "INVALID_DATA_GIVEN");
    148.                         }
    149.                         string username = args[1];
    150.                         string password = args[2];
    151.  
    152.                         Console.WriteLine("[" + ip + "] Using credentials: " + username + " and " + password);
    153.  
    154.                         string response = GetAsync(("https://goldenarmy.eu/studios/id/cman/clogin.php?name=" + username + "&password=" + password));
    155.                         Console.WriteLine("https://goldenarmy.eu/studios/id/cman/clogin.php?name=" + username + "&password=" + password);
    156.                         Console.WriteLine(response);
    157.  
    158.                         if (response.Equals("ERROR_FAILED_CONNECTION"))
    159.                         {
    160.                             SendMessage(stream, "ERROR_SERVERERROR");
    161.                         }
    162.                         else if (response.Equals("INVALID_USERNAME"))
    163.                         {
    164.                             SendMessage(stream, "INVALID_DATA_GIVEN");
    165.                         }
    166.                         else if (response.Equals("INVALID_PASSWORD"))
    167.                         {
    168.                             Console.WriteLine("INVALID_DATA_GIVEN");
    169.                             SendMessage(stream, "INVALID_DATA_GIVEN");
    170.                         }
    171.                         else if (response.Equals("INVALID_CRED"))
    172.                         {
    173.                             Console.WriteLine("ERROR_AUTH_INVALID_CRED");
    174.                             SendMessage(stream, "ERROR_AUTH_INVALID_CRED");
    175.                         }
    176.                         else if (response.Equals("IS_BANNED"))
    177.                         {
    178.                             Console.WriteLine("ERROR_AUTH_BANNED");
    179.                             SendMessage(stream, "ERROR_AUTH_BANNED");
    180.                         }
    181.                         else
    182.                         {
    183.                             Console.WriteLine("[" + ip + "] LOGIN OK | RESPONSE: " + response);
    184.                             SendMessage(stream, response);
    185.                             clientSession = response;
    186.                         }
    187.  
    188.                         continue;
    189.                     }
    190.  
    191.                     if ((!responseData.StartsWith("auth") || !responseData.StartsWith("useauthtoken") || !responseData.StartsWith("close")) && clientSession.Equals("NULL"))
    192.                     {
    193.                         SendMessage(stream, "ERROR_AUTH_MISSING");
    194.                         continue;
    195.                     }
    196.  
    197.                     if (responseData.Equals("endGame"))
    198.                     {
    199.                         char c = '|';
    200.                         string[] args = responseData.Split(c);
    201.  
    202.                         if (!(args.Length > 3))
    203.                         {
    204.                             SendMessage(stream, "INVALID_DATA_GIVEN");
    205.                         }
    206.  
    207.                         string won = args[0];
    208.                         string kills = args[1];
    209.                         string singleplayer = args[2];
    210.  
    211.                         string response = GetAsync("https://goldenarmy.eu/studios/id/cman/cgameend.php?session=" + client + "&won=" + won + "&kills=" + kills + "&singleplayer=" + singleplayer);
    212.  
    213.                         if (response.Equals("ERROR_FAILED_CONNECTION"))
    214.                         {
    215.                             SendMessage(stream, "ERROR_SERVERERROR");
    216.                         }
    217.                         else if (response.Equals("SESSION_INVALID"))
    218.                         {
    219.                             SendMessage(stream, "ERROR_AUTH_MISSING");
    220.                         }
    221.                         else if (response.Equals("SUCCESS"))
    222.                         {
    223.                             SendMessage(stream, "SUCCESS");
    224.                         }
    225.                     }
    226.  
    227.  
    228.                 }
    229.                 catch (Exception)
    230.                 {
    231.                     Console.WriteLine("[" + ip + "] Connection closed.");
    232.                     break;
    233.                 }
    234.             }
    235.         }
    236.  
    237.         public static string GetAsync(string uri, Action<WebHeaderCollection> headers = null)
    238.         {
    239.             HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
    240.             headers?.Invoke(request.Headers);
    241.             request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
    242.  
    243.             using (HttpWebResponse response = (HttpWebResponse) request.GetResponse())
    244.             using (Stream stream = response.GetResponseStream())
    245.             using (StreamReader reader = new StreamReader(stream))
    246.             {
    247.                 return reader.ReadToEnd();
    248.             }
    249.         }
    250.  
    251.         public static void SendMessage(NetworkStream stream, string msg)
    252.         {
    253.             Byte[] sendBytes = Encoding.ASCII.GetBytes(msg);
    254.             stream.Write(sendBytes, 0, sendBytes.Length);
    255.         }
    256.  
    257.     }
    LoginMenuHandler (Unity):


    Code (CSharp):
    1. using System.Collections;
    2.     using System.Collections.Generic;
    3.     using System.Net.Sockets;
    4.     using UnityEngine;
    5.     using UnityEngine.UI;
    6.     using UnityEngine.UIElements;
    7.     using UnityEngine.SceneManagement;
    8.  
    9.     public class LoginMenuHandler : MonoBehaviour
    10.     {
    11.  
    12.         public InputField txtUsername = null;
    13.         public InputField txtPassword = null;
    14.         public Text result = null;
    15.  
    16.         System.Net.Sockets.TcpClient clientSocket = new System.Net.Sockets.TcpClient();
    17.  
    18.         void Start()
    19.         {
    20.             clientSocket.Connect("goldenarmy.eu", 10250);
    21.  
    22.             /*if((PlayerPrefs.GetString("authtoken_v2").Length > 15))
    23.             {
    24.                 authTokenLogin();
    25.             }*/
    26.         }
    27.  
    28.         void Update()
    29.         {
    30.          
    31.         }
    32.  
    33.         void OnApplicationQuit()
    34.         {
    35.             NetworkStream serverStream = clientSocket.GetStream();
    36.             byte[] outStream = System.Text.Encoding.ASCII.GetBytes("close");
    37.             serverStream.Write(outStream, 0, outStream.Length);
    38.             serverStream.Flush();
    39.         }
    40.  
    41.         public void Login()
    42.         {
    43.             NetworkStream serverStream = clientSocket.GetStream();
    44.             byte[] outStream = System.Text.Encoding.ASCII.GetBytes("auth|" + txtUsername.text.Trim() + "|" + txtPassword.text.Trim());
    45.             serverStream.Write(outStream, 0, outStream.Length);
    46.             serverStream.Flush();
    47.  
    48.             byte[] inStream = new byte[2048];
    49.             serverStream.Read(inStream, 0, inStream.Length);
    50.             string returndata = System.Text.Encoding.ASCII.GetString(inStream);
    51.  
    52.             if (returndata.Equals("INVALID_DATA_GIVEN"))
    53.             {
    54.                 result.text = "You gave in invalid data.";
    55.             }
    56.             else if (returndata.Equals("ERROR_AUTH_INVALID_CRED"))
    57.             {
    58.                 result.text = "Username or password incorrect.";
    59.                 print("error invalid data");
    60.             }
    61.             else if (returndata.Equals("ERROR_AUTH_BANNED"))
    62.             {
    63.                 result.text = "Your account got banned. For further detail, please contact our support.";
    64.             }
    65.             else if (returndata.Equals("ERROR_SERVERERROR"))
    66.             {
    67.                 result.text = "An internal server error occurred. We apologize.";
    68.             }else {
    69.                 PlayerPrefs.SetString("authtoken_v2", returndata);
    70.                 result.text = returndata;
    71.                 //serverStream = clientSocket.GetStream();
    72.                 //outStream = System.Text.Encoding.ASCII.GetBytes("close");
    73.                 //serverStream.Write(outStream, 0, outStream.Length);
    74.                 //serverStream.Flush();
    75.                 //clientSocket.GetStream().Close();
    76.                 //clientSocket.Close();
    77.                 //SceneManager.LoadScene("menu");
    78.             }
    79.         }
    80.  
    81.         void authTokenLogin()
    82.         {
    83.             NetworkStream serverStream = clientSocket.GetStream();
    84.             byte[] outStream = System.Text.Encoding.ASCII.GetBytes("useauthtoken|" + PlayerPrefs.GetString("authtoken"));
    85.             serverStream.Write(outStream, 0, outStream.Length);
    86.             serverStream.Flush();
    87.  
    88.             byte[] inStream = new byte[8192];
    89.             serverStream.Read(inStream, 0, inStream.Length);
    90.             string returndata = System.Text.Encoding.ASCII.GetString(inStream);
    91.  
    92.             if (returndata.Equals("OK"))
    93.             {
    94.                 serverStream = clientSocket.GetStream();
    95.                 outStream = System.Text.Encoding.ASCII.GetBytes("close");
    96.                 serverStream.Write(outStream, 0, outStream.Length);
    97.                 serverStream.Flush();
    98.                 clientSocket.GetStream().Close();
    99.                 clientSocket.Close();
    100.                 SceneManager.LoadScene("menu");
    101.             }
    102.         }
    103.     }
    And I am getting "ERROR_AUTH_INVALID_CRED" as text in the Text result, defined in the object that the script is attached to.


    [Image]: https://goldenarmy.eu/studios/unity/imgs/error.png
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    You should just use a password which you will change after you show us instead of censoring critical information to your question. For all I know your password check is failing because it isn't in the ascii character set, since you're using System.Text.Encoding.ASCII.GetBytes in several places. Don't know because you are censoring exactly the information involved in what is failing.

    When people see a bunch of important information to a question being held back, they often just walk away from the thread unfortunately.

    Besides that, check for things like unexpected trailing white space.
     
    Last edited: Aug 2, 2019
  3. GoldenretriverYT

    GoldenretriverYT

    Joined:
    May 8, 2019
    Posts:
    13
    It normally shouldn't be a encoding problem, since the password and username are ascii_bin coallation in the database