【问题标题】:Username and password not case sensitive用户名和密码不区分大小写
【发布时间】:2019-09-16 18:41:33
【问题描述】:

我知道我没有将我的密码存储在哈希中,但对于这个数据库来说,这并不重要。

代码引用了一个将密码存储为纯文本的 SQL 数据库

代码如下:

/*Checks to see if the username and password matcch the database
         If it does, it lets you in, if not you displays an error message*/
        string user = textBox1.Text.ToString();
        string pass = textBox2.Text;
        MySqlConnection conn = new MySqlConnection(ConnectionString);
        MySqlDataAdapter sda = new MySqlDataAdapter("SELECT COUNT(*) from Employees WHERE UserName = '"+(user)+ "' and Password = '"+(pass)+"' collate Latin1_Genral_CS_AS", conn);
        DataTable dt = new DataTable();
        sda.Fill(dt);
        if(dt.Rows[0][0].ToString() == "1")
        {
            HomeScreen home = new HomeScreen();
            this.Hide();
            home.ShowDialog();
        }
        else
        {
            MessageBox.Show("Incorrect Username or Password", "alter", MessageBoxButtons.OK, MessageBoxIcon.Error );
        }            

【问题讨论】:

  • 那么你的问题是什么? BTW Latin1_Genral_CS_AS(原文如此!请注意一般缺少 e)不是有效的排序规则

标签: sql database passwords


【解决方案1】:

您可以将输入值和表格列都转换为lowercase 或uppercase:

/*Checks to see if the username and password matcch the database
         If it does, it lets you in, if not you displays an error message*/
        string user = textBox1.Text.ToLower();
        string pass = textBox2.Text.ToLower();
        MySqlConnection conn = new MySqlConnection(ConnectionString);
        MySqlDataAdapter sda = new MySqlDataAdapter("SELECT COUNT(*) from Employees WHERE LOWER(UserName) = '"+(user)+ "' and LOWER(Password) = '"+(pass)+"' collate Latin1_Genral_CS_AS", conn);
        DataTable dt = new DataTable();
        sda.Fill(dt);
        if(dt.Rows[0][0].ToString() == "1")
        {
            HomeScreen home = new HomeScreen();
            this.Hide();
            home.ShowDialog();
        }
        else
        {
            MessageBox.Show("Incorrect Username or Password", "alter", MessageBoxButtons.OK, MessageBoxIcon.Error );
        }   

【讨论】:

  • 不用转换,可以用查询指定排序规则。
  • 是的,这可能是另一种解决方案。
【解决方案2】:

你可以alter你的database使用你想要的collation:

ALTER DATABASE DBNAME CHARACTER SET utf8 COLLATE utf8_general_ci;

但是,这仅适用于新表。您可以alter 和table column 使用您喜欢的collation:

ALTER TABLE table_name MODIFY column_name column_datatype COLLATE utf8_bin;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-12-06
    • 2020-05-01
    • 2011-09-25
    • 2016-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多