简体   繁体   English

将用户输入的用户名/密码与mysql数据库的用户名/密码进行比较

[英]Comparing user input username/password to mysql database username/password not working

I'm having trouble comparing the input username and password with the username and password of the specified user in the database. 我无法将输入的用户名和密码与数据库中指定用户的用户名和密码进行比较。 I believe all the code is correct but when I click the login button, nothing happens. 我相信所有代码都是正确的,但是当我单击“登录”按钮时,没有任何反应。 Help please, if you need any additional information, just ask please. 请提供帮助,如果您需要任何其他信息,请询问。

Code: 码:

private void btnLogin_Click(object sender, EventArgs e)
    {
        int numerror = 0;
        if (UsernameTextBox.Text == "")
        {
            numerror = numerror + 1;
        }
        if (PasswordTextBox.Text == "")
        {
            numerror = numerror + 1;
        }
        if (numerror == 1)
        {
            ErrorLabel.Text = "*1 required field is blank.";
        }
        else if (numerror == 2)
        {
            ErrorLabel.Text = "*2 required fields are blank";
        }
        else
        {
            string connectionString = "datasource=localhost;port=3306;username=*****;password=**********";
            string select = "SELECT username, password FROM userinfo.users " +
                            "WHERE username = @username AND password = @password";

            using (MySqlConnection Conn = new MySqlConnection(connectionString))
            {
                using (MySqlCommand cmd = new MySqlCommand(select, Conn))
                {
                    cmd.Parameters.AddWithValue("@username", UsernameTextBox.Text);
                    cmd.Parameters.AddWithValue("@password", PasswordTextBox.Text);

                    Conn.Open();

                    using (MySqlDataReader reader = cmd.ExecuteReader())
                    {
                        if (reader.Read())
                        {
                            string username = reader.GetString(0);
                            string password = reader.GetString(1);

                            if (username == UsernameTextBox.Text)
                            {
                                string encodeduserinputpassword = EncodePassword(PasswordTextBox.Text);
                                if (password == encodeduserinputpassword)
                                {
                                    AirSpace airspaceform = new AirSpace();
                                    airspaceform.Show();
                                    this.Hide();
                                }
                                else
                                {
                                    CMessageBox("Login Error", "Incorrect username or password.");
                                }
                            }
                            else
                            {
                                CMessageBox("Login Error", "Incorrect username or password.");
                            }
                        }
                    }

                    Conn.Close();
                }
            }
        }
    }

You are using an encoded password via EncodePassword but your select query is doing a straight match. 您正在通过EncodePassword使用编码密码,但您的选择查询进行了直接匹配。

Change your select to 将选择更改为

SELECT username, password FROM userinfo.users
WHERE username = @username

and remove the password parameter. 并删除密码参数。

private void btnLogin_Click(object sender, EventArgs e)
{
    int numerror = 0;
    if (UsernameTextBox.Text == "")
    {
        ErrorLabel.Text = "*1 required field is blank.";
    }
    else if (PasswordTextBox.Text == "")
    {
        ErrorLabel.Text = "*2 required fields are blank";
    }
    else
    {
        string connectionString = "datasource=localhost;port=3306;username=*****;password=**********";
        string select = "SELECT username, password FROM userinfo.users " +
                        "WHERE username = @username";

        using (MySqlConnection Conn = new MySqlConnection(connectionString))
        {
            using (MySqlCommand cmd = new MySqlCommand(select, Conn))
            {
                cmd.Parameters.AddWithValue("@username", UsernameTextBox.Text);

                Conn.Open();

                using (MySqlDataReader reader = cmd.ExecuteReader())
                {
                    if (reader.Read())
                    {
                        string username = reader.GetString(0);
                        string password = reader.GetString(1);

                        string encodeduserinputpassword = EncodePassword(PasswordTextBox.Text);
                        if (password == encodeduserinputpassword)
                        {
                            AirSpace airspaceform = new AirSpace();
                            airspaceform.Show();
                            this.Hide();
                        }
                        else
                        {
                            CMessageBox("Login Error", "Incorrect username or password.");
                        }
                    }
                    else
                    {
                        CMessageBox("Login Error", "Incorrect username or password.");
                    }
                }

                Conn.Close();
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用用户名和密码将用户连接到数据库 - Connecting user to database with username and password 用户名/密码数据库检查 - Username/Password Database Checking 查询数据库的用户名和密码 - Querying a database for username and password 用户名和密码输入代码验证 - Username and password input code validation 数据库到#c(用户名和密码)->错误 - Database to #c (username & password) -> error 从数据库读取用户名和密码 - Reading username and password from database 如何使用C#创建用户oracle数据库而不使用SQL手动输入用户名,密码? - How to create user oracle database using C# not using SQL manually input username,password? 如何使用Xamarin中的用户输入验证sqlite用户名和密码 - how to validate sqlite username & password with user input in xamarin 使用方法“mysql_native_password”对用户“用户名”的主机“192.168.0.31”进行身份验证失败,并显示消息:未知数据库“mysql57” - Authentication to host '192.168.0.31' for user 'username' using method 'mysql_native_password' failed with message: Unknown database 'mysql57' 我正在处理仅包含 3 个字段的新用户注册表单,用户名、密码和确认密码 - I am working on a New user registration form that only contains of 3 fields, Username, password and confirm password
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM