Click here to Skip to main content
16,013,338 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi , I have a Login page and want that if the user credentials provided are those of the Administrator(Username = Administrator) then a Window Form App Page called "AdminPage" should open up. Else another Window Form App called "Main_Page" should open up.

What I have tried:

try
           {
               SqlConnection cn = new SqlConnection("Data Source=PV10\\LOCALSERVER;Initial Catalog=SmallSoftwareDB;Integrated Security=True;Pooling=False");
               SqlCommand cmd = new SqlCommand("select * from UserCredentials where Username='" + textBox1.Text + "' and Password='" + textBox2.Text + "'", cn);
               SqlDataReader dr;
               cn.Open();
               dr = cmd.ExecuteReader();
               int cnt = 0;
               while (dr.Read())
               {
                   cnt++;
               }
               if (cnt == 1)
               {
                   MessageBox.Show("Successful Login...", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
                   string query = "select Username, Password from UserCredentials where Username='Administrator";
                   SqlCommand cmdA = new SqlCommand(query, cn);
                   dr = cmdA.ExecuteReader();
                   int k = 0;
                   while (dr.Read())
                   {
                       k++;
                   }
                   if (k == 1)
                   {
                       AdminPage A_P = new AdminPage();
                       A_P.Tag = this;
                       A_P.Show(this);
                       Hide();
                   }
                   Main_Page Mp = new Main_Page();
                   Mp.Tag = this;
                   Mp.Show(this);
                   Hide();
                   cn.Close();
                   textBox1.Clear();
                   textBox2.Clear();
               }
               else
               {
                   MessageBox.Show("Invalid UserName or Password", "Message", MessageBoxButtons.RetryCancel, MessageBoxIcon.Warning);
                   textBox1.Clear();
                   textBox2.Clear();
               }
           }
           catch (Exception err)
           {
               MessageBox.Show(err.Message, " ", MessageBoxButtons.OK, MessageBoxIcon.Warning);
           }
Posted
Updated 17-Nov-16 0:35am
v2
Comments
Richard Deeming 21-Sep-16 12:55pm    
You're also storing passwords in plain text. That is an extremely bad idea. You should only ever store a salted hash of the user's password, using a unique salt per record.

Secure Password Authentication Explained Simply[^]
Salted Password Hashing - Doing it Right[^]

Your code is executing

select Username, Password from UserCredentials where Username='Administrator'


which is always going to return you a result regardless of what the user's credentials are, so it will think everyone is admin. If the username being Administrator is the only thing that indicates they are admin then simply do a check for that;

C#
if (textBox1.Text.Equals("Administrator", StringComparison.InvariantCultureIgnoreCase))
{
    // user is admin
}


Also look at using parameterised queries rather than creating your sql queries via string concatenation as your code is vulnerable to SQL injection attacks. Also rather than doing a while\read loop and incrementing a count you can just check "if (dr.Read())" as it doesn't really matter how many results there are. You can also do a "select count(*) from table where...." and then do an ExecuteScalar call to read the result of the count and see if it is 0 or 1, or whatever.
 
Share this answer
 
Comments
Nganku Junior 23-Sep-16 12:44pm    
Thanks for the heads-up. I used
if (textBox1.Text.Equals("Administrator", StringComparison.InvariantCultureIgnoreCase))
{
// user is admin
}
To get the job done.
C#
if (textBox1.Text.Equals("Administrator", StringComparison.InvariantCultureIgnoreCase))
{
// user is admin
}
 
Share this answer
 

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