L.redir
L.redir

Reputation: 65

How to match user credentials against text file contents in C#

I am trying to create a login method that checks user credentials against a text file called "accounts.txt" that has a strings such as "Bob password" already logged in it.

The login method parameters are like - "login" is the command, username (e.g. "Bob") is param1 and password (e.g. "password*44") is param2.

When I run command line arguments, an example would be "login Bob password" and the method should read contents of the file line by line until it finds a match and returns "login Bob successful". Otherwise it'll say "invaild username/password".

I'm not sure how to go about this, any tips are appreciated. This is my current code

protected void login(string command, string param1, string param2) // string command "login" string username, string password
{
     // login command
     // Needs to check accounts.txt file for username and password.
     // if username and password exists, provide logon message
     // if username and/ or password does not exist, provide failed logon message.
     // IN MEMORY store current login username and password

     //checks accounts.txt file if username already exists and if there is a match

     if (not sure what arg would go here)
     {
         string path = "C:\\Files\\accounts.txt";
         Console.WriteLine("username and password match", path);
     }
     else
     {
         Console.WriteLine("Login Failed: invaild username or password");
         string path2 = "C:\\Files\\audit.txt";
         using (StreamWriter sw2 = File.AppendText(path2))
         {
              sw2.WriteLine("Login Failed: Invaild username or password");
         }
         throw new FileNotFoundException();
 }

Upvotes: 0

Views: 1461

Answers (3)

Arghya C
Arghya C

Reputation: 10078

Your method would look like

protected void login(string command, string param1, string param2) // string command "login" string username, string password
{
    if (command == "login")
    {
        var logins = File.ReadAllLines(@"C:\\Files\\accounts.txt");
        if (logins.Any(l => l == string.Format("{0} {1}", param1, param2)))
            Console.WriteLine("login {0} successful", param1);
        else
        {
            //log audit details
            Console.WriteLine("invaild username/password");
        }
    }
}

Upvotes: 1

ThatBrianDude
ThatBrianDude

Reputation: 3190

As @Valentin already said you can read all lines of a text file with File.ReadAllLines().

string path = "C:\\Files\\accounts.txt";
string[] LoginCredentials = File.ReadAllLines(path);
for(i=0;i<LoginCredentials.Length;i++)
{
    //Split Line (Credential[0] = Username, Credential[1] = Password)
    string[] Credential = LoginCredentials[i].Split(' ');

    //Check if input matches
    if(param1 == Credential[0] && param2 == Credential[1])
    {
       Console.WriteLine("username and password match", path); 
    }
}
   // If Input never matched login failed
    Console.WriteLine("Login Failed: invaild username or password");
    string path2 = "C:\\Files\\audit.txt";
    using (StreamWriter sw2 = File.AppendText(path2))
    {
    sw2.WriteLine("Login Failed: Invaild username or password");
    }
    throw new FileNotFoundException();

Upvotes: 0

Valentin
Valentin

Reputation: 5488

You can read all lines in one array and then try to find your string in this array.

string textToFind = string.Format ("{0} {1} {2}",command,param1,param2)
bool anyHit = File.ReadAllLines (path).Any(x => string.Compare (textToFind,x) == 0));

Upvotes: 1

Related Questions