0

What should i do to make this case sensitive?

When I try this code with input in different cases but with the same spelling of username and password, it still logs in. My fields are all varchar in database.

if (user_txt.Text != "" & pass_txt.Text != "")
{
    string queryText = "SELECT Count(*) FROM stiguidancesample.users " + "WHERE username = @Username AND password = @Password";
    MySqlConnection cn = new MySqlConnection(MyConnectionString);
    MySqlCommand cmd = new MySqlCommand(queryText, cn);

    {
        cn.Open();
        cmd.Parameters.AddWithValue("@Username", user_txt.Text);  // cmd is SqlCommand 
        cmd.Parameters.AddWithValue("@Password", pass_txt.Text);
        int result = Convert.ToInt32(cmd.ExecuteScalar());
        if (result > 0)
            MessageBox.Show("Loggen In!");
        else
            MessageBox.Show("User Not Found!");
    }
}
Richard Schwartz
  • 14,463
  • 2
  • 23
  • 41
davz_11
  • 135
  • 2
  • 6
  • 14

3 Answers3

0

I don't know if all databases are the same but SQL Server and MySQL are both apparently case-insensitive when comparing for text equality. That's as it should be for a login. It shouldn't matter what case the user enters their user name in; it should still match.

As for the password, you should not be saving it directly so it shouldn't matter. You should be hashing the password and storing that. The chances of anyone entering a password value that leads to text containing the same characters in a different case as the hash that you have stored in the database is next to nothing.

To expand on that, you hash the password and save the result when the user registers and then, when they login, you hash the password they provide and compare that to the value in the database. If the hash values match then the passwords are considered to match.

jmcilhinney
  • 50,448
  • 5
  • 26
  • 46
0

The collation is relevant in how the strings are compared. See for e.g. this answer here: How can I make SQL case sensitive string comparison on MySQL?

or read the manual here: http://dev.mysql.com/doc/refman/5.0/en/case-sensitivity.html

Community
  • 1
  • 1
Ulrich Thomas Gabor
  • 6,584
  • 4
  • 27
  • 41
0

Your code should be like below

if (user_txt.Text != "" & pass_txt.Text != "")
{
    string queryText = "SELECT Count(*) FROM stiguidancesample.users " +  "WHERE       username = @Username AND password = @Password COLLATE SQL_Latin1_General_CP1_CS_AS ";
MySqlConnection cn = new MySqlConnection(MyConnectionString);
MySqlCommand cmd = new MySqlCommand(queryText, cn);

  {
    cn.Open();
    cmd.Parameters.AddWithValue("@Username", user_txt.Text);  // cmd is SqlCommand 
    cmd.Parameters.AddWithValue("@Password", pass_txt.Text);
    int result = Convert.ToInt32(cmd.ExecuteScalar());
    if (result > 0)
        MessageBox.Show("Loggen In!");
    else
        MessageBox.Show("User Not Found!");
  }
}

Just add

COLLATE SQL_Latin1_General_CP1_CS_AS

after your @Password in query text.

Sumon Tito
  • 105
  • 10