Home > Back-end >  C# Login error with text checking/validation
C# Login error with text checking/validation

Time:03-08

So baiscally my first registered username and password in my text file when i login is able to pass through the validation however my next pair gets past the .contains but fails at checking password, not giving me the message and closing the console.

Example of text file-

Elliot|Benten67Fred|Payne

It will be able to check Elliot and Benten67. However can locate Fred but not able to check if the password is right? Any help would be great

public void Register()
        {
            string filePath = @"/Users/elliot/Documents/Film Libary Software/Film-Libary/Film-Libary/bin/Debug/netcoreapp3.1/UserInfo.txt";


            Console.WriteLine("Create A Username which contains A Upper Case character");


            Username = Console.ReadLine();
            bool usernameIsUpper = Username.Any(char.IsUpper);
            if (usernameIsUpper)
            {
                Console.WriteLine("Please enter a password: ");
                Password = Console.ReadLine();
                string toWrite = Username   "|"   Password;
                File.AppendAllText(filePath, toWrite);
                Login();


            }
            else
            {
                Console.WriteLine("Invalid Username: Please try again!");
                Register();
            }






        }

        public void Login()
        {
            string fileName = @"/Users/elliot/Documents/Film Libary Software/Film-Libary/Film-Libary/bin/Debug/netcoreapp3.1/UserInfo.txt";

            using (StreamReader reader = new StreamReader(fileName))
            {
                string line;
                Console.WriteLine("Enter your username");
                string loginUsername = Console.ReadLine();
                while ((line = reader.ReadLine()) != null)
                {
                    if (line.Contains(loginUsername))
                    {
                        int indexOfDelimiter = line.IndexOf('|');
                        string usernameFromFile = loginUsername;
                        string passwordFromFile = line.Substring(indexOfDelimiter   1, line.Length - (indexOfDelimiter   1));
                        Console.WriteLine("Please enter your password");
                        string enteredPassword = Console.ReadLine();
                        if (enteredPassword == passwordFromFile)
                        {
                            Console.WriteLine("Elliot well done");
                        }

                    }


                }

            }
        }

CodePudding user response:

Your reading code requires the each user be on a separate line, but you are not writing new lines, you can see that here

Elliot|Benten67Fred|Payne

you need

Elliot|Benten67
Fred|Payne

The error is here

            string toWrite = Username   "|"   Password;
            File.AppendAllText(filePath, toWrite);

you need

            string toWrite = Username   "|"   Password   "\n";
            File.AppendAllText(filePath, toWrite);
  • Related