Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So I have this code to Authentic authenticate my user but it only loop through the first user and and other username it gives me wrong password or username. Any help please


What I have tried:

XmlDocument mac = new XmlDocument();
               mac.Load(@"C:\Users\user\source\repos\lilis shop\lilis shop\Properties\XMLFile1.xml");
               XmlNodeList xnlist = mac.SelectNodes("//users");

               foreach (XmlNode naa in xnlist)
               {
                   string username = naa["username"].InnerText;
                   string password = naa["password"].InnerText;

                   if (username == txtusername.Text && password == txtpassword.Text )
                   {
                       Letsee = "LOGEDIN";
                       Form1 openform = new Form1();
                       openform.lablogin.Text = Letsee;
                       openform.lab1.Text = this.txtusername.Text;
                       openform.labelsingout.Text = "SINGOUT";

                       openform.Show();

                       this.Hide();
                       break;
                   }
                    else if (username != txtusername.Text)
                   {
                       MessageBox.Show("please enter the correct username", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                       txtpassword.Text = null;
                       txtusername.Text = null;
                       break;

                   }
                   else if (password != txtpassword.Text)
                   {
                       MessageBox.Show("please enter the correct password", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                       txtpassword.Text = null;
                       txtusername.Text = null;
                       break;

                   }

                   else
                   {
                       MessageBox.Show("please enter the correct password or username", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                       txtpassword.Text = null;
                       txtusername.Text = null;
                       break;


                   }
Posted
Updated 9-May-20 13:33pm

1 solution

I see multiple problems with your code:

The actual problem you are asking about is because every branch of your of your if...then block ends with a break which stops the loop. The only way this would validate someone is if the first username/password combination is the correct one.

The next problem is the security of the this; your passwords are being stored in plain text. The preferred way to save passwords is with a salted hash, second place to this would be use some sort of encryption.

Another security related problem is that it is too helpful with the responses. You should only tell someone if their login is valid or invalid. Telling someone their password is wrong is also telling them they have a valid username. A hacker would then have half the combination to get in.

Also.. the logic is just wrong. Way too many else if combinations in there; when all you need is a nested IF:
for loop {
   If username matches {
      if password matches {
         login succeeded
         break loop
      }
   }
   // continue through loop
}
// loops done: If we didn't succeed by now, we failed. No THEN needed
So you should end up with something like this
C#
XmlDocument mac = new XmlDocument();

mac.Load(@"C:\Users\user\source\repos\lilis shop\lilis shop\Properties\XMLFile1.xml");
XmlNodeList xnlist = mac.SelectNodes("//users");

foreach (XmlNode naa in xnlist)
{
   string username = naa["username"].InnerText;
   string password = naa["password"].InnerText; 

   if (username == txtusername.Text) {
      if (password == txtpassword.Text) {
         Letsee = "LOGEDIN";
         Form1 openform = new Form1();
         openform.lablogin.Text = Letsee;
         openform.lab1.Text = this.txtusername.Text;
         openform.labelsingout.Text = "SINGOUT";

         openform.Show();

         this.Hide();
         break;
      }
   }
}
MessageBox.Show("Login Failed", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
txtpassword.Text = null;
txtusername.Text = null;
 
Share this answer
 
Comments
[no name] 10-May-20 9:31am    
+5
MadMyche 10-May-20 9:39am    
Thank you
Weeman Osekre 11-May-20 7:18am    
Thanks man but this code is still only recognizing the first user and other user is incorrect
MadMyche 11-May-20 8:06am    
I would recommend setting a breakpoint at the beginning of the loop, and stepping through it to see what values are going on where. Pay close attention when you think you have a match going into it

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900