C# 显示用户名或密码是否不正确?
描述并显示C# 显示用户名或密码是否不正确?,c#,login,C#,Login,描述并显示用户名或密码是否不正确 我当前的代码: public partial class Form1 : Form { SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\AKO Cc\Desktop\visual\WindowsFormsApplication7\WindowsFormsApplication7\Database1.mdf;Integ
用户名
或密码
是否不正确
我当前的代码:
public partial class Form1 : Form
{
SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\AKO Cc\Desktop\visual\WindowsFormsApplication7\WindowsFormsApplication7\Database1.mdf;Integrated Security=True");
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
con.Open();
SqlCommand sql=new SqlCommand("select * from db where username='"+textBox1.Text+"' and password='"+textBox2.Text+"'",con);
SqlDataReader rs=sql.ExecuteReader();
if (rs.HasRows) {
rs.Read();
string id = rs.GetValue(0).ToString();
string type = rs.GetValue(3).ToString();
MessageBox.Show(id + " " + type);
}
rs.Close();
con.Close();
}
}
首先,您有一个易受攻击的设计:当db
表被盗时,所有登录/密码都会被压缩。不存储密码,但存储其哈希
接下来,让我们提取一个方法(单独的业务逻辑-credentail验证和UI-TextBox
es):
然后您可以轻松地使用它:
private void button1_Click(object sender, EventArgs e) {
if (IsCredentialValid(textBox1.Text, textBox2.Text)) {
// username and password are correct
}
else {
// username or password is incorrect
}
}
你的帖子缺少一个问题。请添加一个详细的问题。我通常认为这样做是“用户名或密码无效”或类似的。请注意,您应该尝试用户名
”或1=1;——代码>使用您的应用程序,然后考虑使用参数化SQL查询。只需告诉用户登录失败。如果指定失败的是用户名还是密码,则可能会向未经授权的用户提供超出必要范围的信息(例如,他们拥有有效用户名,但密码无效,因此可以强制输入密码,因为他们已经拥有有效用户名).出于安全原因,强烈建议不要说明用户或pw是否错误。你总是说两者的结合是错误的。另外,你的应用程序目前不需要密码:)@ParezDara:这个答案还包含使用参数来避免SQL注入。在WinForms应用程序中,安全性可能不如在网站中那么重要,但最好尽早养成使用它们的习惯。
private void button1_Click(object sender, EventArgs e) {
if (IsCredentialValid(textBox1.Text, textBox2.Text)) {
// username and password are correct
}
else {
// username or password is incorrect
}
}