asiawatcher
asiawatcher

Reputation: 41

Reading a remote text file line by line and comparing with inputbox entry

I wrote an application with an inputbox where I want people to input password which will be compared from a list of passwords stored in my webserver in a text file which are one entry in each line then given access to my application

So in a few words i want the inputbox password to be compared line by line to my text file but i have failed to do that so far

Here is my code:

string input = 
    Microsoft.VisualBasic.Interaction.InputBox("Please enter your password for access to this software", "pass:");

if (input=="")
{
    appexit();
}

WebClient client = new WebClient();
Stream stream = client.OpenRead("http://haha.com/access.txt");
StreamReader reader = new StreamReader(stream);
//String content = reader.ReadToEnd();

int counter = 0;
string line;

while ((line = reader.ReadLine()) != null)
{
    if (line!=input)
    {
        MessageBox.Show("This software has been deactivated because of wrong pass", "YOUR ACCESS HAS BEEN LIMITED");
        appexit();
    }

    counter++;
}

reader.Close();

The password file contains lines such as:

hahdfdsf 
ha22334rdf 
ha2233gg 
charlysv-es

Where is the error? The code compiles but even if the correct password is entered, the check fails.

Upvotes: 1

Views: 1351

Answers (1)

lezhkin11
lezhkin11

Reputation: 414

According to your loop, when you get a line that doesn't equal input, then you stop everything - what is logically incorrect. You have to compare lines, until one of them equals input or end of file.

 ...
 bool valid = false;

 using (WebClient client = new WebClient())
 {
     using (Stream stream = client.OpenRead("http://haha.com/access.txt"))
     {
         using (StreamReader reader = new StreamReader(stream))
         {
             string line;

             while ((line = reader.ReadLine()) != null)
             {
                 if (line.Equals(input))
                 {
                     valid = true;
                     break;
                 }
             }
         }
     }
 }

 if (valid)
 {
     // password is correct
     ...
 }
 else
 {
    MessageBox.Show("This software has been deactivated because of wrong pass", "YOUR ACCESS HAS BEEN LIMITED");
    appexit();
 }
 ...

Upvotes: 3

Related Questions