【发布时间】:2020-09-03 08:53:40
【问题描述】:
所以我制作了一个表单,您可以在其中从数据库登录。代码应该是不言自明的。
private void button1_Click(object sender, EventArgs e)
{
try
{
string MyConnection = "datasource=localhost;port=3306;username=root;password=xdmemes123";
MySqlConnection myConn = new MySqlConnection(MyConnection);
MySqlCommand SelectCommand = new MySqlCommand("select * from life.players where DBname='" + this.username.Text + "' and DBpass='" + this.password.Text +"' ; ", myConn);
MySqlDataReader myReader;
myConn.Open();
myReader = SelectCommand.ExecuteReader();
int count = 0;
while (myReader.Read())
{
count = count + 1;
}
if (count == 1)
{
Properties.Settings.Default.Security = "Secure";
Properties.Settings.Default.AdminName = username.Text;
Properties.Settings.Default.AdminPass = password.Text;
Properties.Settings.Default.Save();
MessageBox.Show("Logged in");
this.Hide();
Form2 f2 = new Form2();
f2.ShowDialog();
}
else if (count > 1)
{
Properties.Settings.Default.Security = "Insecure";
MessageBox.Show("Incorrect!");
}
else
{
Properties.Settings.Default.Security = "Insecure";
MessageBox.Show("Incorrect!");
myConn.Close();
}
}
catch (Exception ex)
{
MessageBox.Show("Something went wrong. Error copied to clipboard.");
Clipboard.SetText(ex.Message);
}
}
但我的问题是,这对 MYSQL 注入是否安全?如果没有,我该怎么做才能保证安全?
如果可能,请编写或解释如何编写此代码。我对这种编码很陌生,但真的很喜欢它,并且想继续我的程序。
【问题讨论】:
-
不,这对于 SQL 注入是不安全的。去阅读它,你会立即明白为什么。
-
这正是SQL注入漏洞的实现方式!
-
this.username.Text == "0'; delete from SomeTables -- " -
您的密码也没有经过哈希处理,这在当今时代也是不可接受的。
标签: c# mysql .net sql-injection