lundi 7 juin 2021

Code from ClassLibary is being skipped (creating a new user)

I'm trying to learn coding. I've tried to make a simple login system by using a project with a reference to my class library where all my "logic" is hidden. The problem now is that the part where it creates a new user/pass and writes it to a file is skipped and it keeps saying "Your informaton has been updated."

namespace Login
{
    class Program
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("Login Page");
            Console.Write("Username: ");
            string Bruger = Console.ReadLine();
            Console.Write("Password: ");
            string Kode = Console.ReadLine();
            Console.WriteLine(ClassLibrary.Login.LoginLogik(Bruger, Kode));

            Console.Write("Old username: ");
            Bruger = Console.ReadLine();
            Console.Write("Old password: ");
            Kode = Console.ReadLine();
            Console.WriteLine(ClassLibrary.Login.LoginLogik(Bruger, Kode));

            Console.Write("New username: ");
            Bruger = Console.ReadLine();
            Console.Write("New password: ");
            Kode = Console.ReadLine();
            Console.WriteLine(ClassLibrary.Login.LoginLogik(Bruger, Kode));
            Console.ReadKey();

        }
    }
}

**CLASSLIBRARY

namespace ClassLibrary
{
   public class Login
    {
        public static string LoginLogik(string Bruger, string Kode)
        
        {
            string svar = "";
            string Path = (@"C:\Users\andrasm\source\repos\Andreas opgaver\ClassLibrary\database.dat");
            var words = File.ReadAllLines(Path);
            string[] databaseArray = File.ReadAllLines(Path);
            string[] data2 = Regex.Split(databaseArray[0], @";");

            if (Bruger == data2[0] && Kode == data2[1])
            {
                svar = "Access granted.";
                //Creating a new user 

                string[] nyLOGINArray = new string[2];
                Regex re = new Regex(@"[A-Z]" + @"[a-z]+");
                data2[0] = Bruger;
                data2[1] = Kode;

                if (re.IsMatch(Kode))
                {
                    String nyLOGINString = string.Join(";", data2);
                    File.WriteAllText(Path, nyLOGINString + Environment.NewLine);
                    svar = "Your informaton has been updated.";
                }
                else
                {
                    svar = "Password doesnt meet requirements";
                }
            }
            else
            {
                svar = "Username or password is wrong, try again.";
            }
            return svar;
        }
    }
}

Aucun commentaire:

Enregistrer un commentaire