检查Mysql DB的用户名和密码C#
几天来一直有这个问题.我正在使用简单的用户名和密码登录屏幕,并将所有内容存储在数据库中.我需要检查用户名和密码是否匹配,并查看帐户是否已激活(0或1).我一直遇到这个问题,似乎无法让它正常工作.任何帮助表示赞赏.
DB Mysql
private void loginButton_Click(object sender, EventArgs e)
{
AdamPanel blarg = new AdminPanel();
string pass, user;
string password = "";
string username = "";
user = usernameBox.Text;
pass = passwordBox.Text;
DataSet bb = new DataSet();
string connectionString = "datasource=stuff;database=users";
MySqlConnection mysql = new MySqlConnection(connectionString);
MySqlDataAdapter adapter = new MySqlDataAdapter();
adapter.SelectCommand = new MySqlCommand("SELECT * FROM RegularUsers WHERE Username = '" + user + "' AND Password = '" + pass + "'", mysql);
adapter.Fill(bb);
if(bb.HasRows)
blarg.Show();
return 0;
}
}
}
仍然没有任何想法吗?
解决方法:
首先,请将您的密码哈希并加盐.确保你的用户名/ pw没有sql注入易受攻击,因为你没有使用带参数化查询的语言…最好使用存储过程,因为我相信MySql现在拥有它们.
然而,尽管如此……我认为这正是你所寻找的.
public int Load()
{
string connectionString = "datasource=STUFF YOU SHOULDNT SEEdatabase=users";
MySqlConnection mysql = new MySqlConnection(connectionString);
MySqlDataAdapter adapter = new MySqlDataAdapter();
mysql.SelectCommand = new MySqlCommand("SELECT * FROM [RegularUsers] WHERE Username = '" + this.username + "' AND Pass = '" + this.password + "'", conn);
mysql.Fill(dataset);
if (dataset.HasRows)
return 1;
return 0;
}