[英]Case Sensitivity in Login Form C#
我的登录表单正在运行,但是当我在单词的第一个字母中输入大写字母的用户名并且数据库中的用户名都小写时,它仍然可以访问并且登录成功。
你能帮我解决这个问题吗? 我真的需要你的帮助 。
这是我当前的代码
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "" || textBox2.Text == "")
{
MessageBox.Show("Input Required Fields!",
"Note",
MessageBoxButtons.OK,
MessageBoxIcon.Exclamation,
MessageBoxDefaultButton.Button1);
}
else
{
//Passing the value from textbox
Tic_Tac_Toe frm2 = new Tic_Tac_Toe(textBox1.Text);
View view = new View(textBox1.Text);
string str = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\Majel\Tic Tac Toe\Database\Database.mdb";
OleDbConnection con = new OleDbConnection(str);
OleDbCommand cmd = new OleDbCommand("SELECT COUNT(*) FROM data WHERE Users = '" + textBox1.Text + "' AND Pass = '" + textBox2.Text + "'", con);
con.Open(); try
{
int i;
i = Convert.ToInt32(cmd.ExecuteScalar());
if (i == 1)
{
MessageBox.Show("Login Successful!",
this.Hide();
frm2.Show();
}
else
{
MessageBox.Show("Invalid User Name or Password",
"Note",
MessageBoxButtons.OK,
MessageBoxIcon.Exclamation,
MessageBoxDefaultButton.Button1);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
con.Close();
}
}
}
有人可以帮我吗? 提前非常感谢您。
很简单,您需要一个WHERE
子句。
字符串SqlString =“更新数据SET Player1 = ?, Player2 =?WHERE UserName = @ user”;
没有它,更新将应用于表中的所有行。 请注意,这与DELETE语句完全相同。
现在,您通常不会在这样的用户名上进行匹配,您将使用表的主键进行此类比较。 除此之外,这是SQL101。强烈建议阅读一本RDBMS书籍,并在尝试进一步学习之前学习SQL的基础知识。
我可以说您要在db中保存密码,您必须使用哈希Algorithm
例如MD5
或SHA1
保存密码数据
当用户键入用于登录的密码时,您会哈希键入密码的字符串,并将此字符串与保存在数据库中的密码进行比较
public static void HashPassword(string Password, out string Salt, out string Hash)
{
System.Security.Cryptography.SHA1Managed sha = new System.Security.Cryptography.SHA1Managed();
Random rnd = new Random();
byte[] s = new byte[20];
rnd.NextBytes(s);
Salt = Convert.ToBase64String(s);
System.Text.UTF8Encoding u = new UTF8Encoding();
byte[] pass = u.GetBytes(Password);
byte[] all = new byte[pass.Length + s.Length];
Array.Copy(pass, all, pass.Length);
Array.Copy(s, 0, all, pass.Length, s.Length);
Byte[] H = sha.ComputeHash(all);
Hash = Convert.ToBase64String(H);
}
public bool IsPasswordCorrect(string Password, string Salt, string Hash)
{
System.Security.Cryptography.SHA1Managed sha = new System.Security.Cryptography.SHA1Managed();
byte[] s = Convert.FromBase64String(Salt);
System.Text.UTF8Encoding u = new UTF8Encoding();
byte[] pass = u.GetBytes(Password);
byte[] all = new byte[pass.Length + s.Length];
Array.Copy(pass, all, pass.Length);
Array.Copy(s, 0, all, pass.Length, s.Length);
Byte[] H = sha.ComputeHash(all);
return (Hash == Convert.ToBase64String(H));
}
现在,您必须使用HashPassword
方法为hash赋予盐,并为每个用户将hash和salt保存到db。
要检查密码时,请使用IsPasswordcorrect
方法
为您的代码
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "" || textBox2.Text == "")
{
MessageBox.Show("Input Required Fields!",
"Note",
MessageBoxButtons.OK,
MessageBoxIcon.Exclamation,
MessageBoxDefaultButton.Button1);
}
else
{
//Passing the value from textbox
Tic_Tac_Toe frm2 = new Tic_Tac_Toe(textBox1.Text);
View view = new View(textBox1.Text);
string str = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\Majel\Tic Tac Toe\Database\Database.mdb";
OleDbConnection con = new OleDbConnection(str);
OleDbCommand cmd = new OleDbCommand("SELECT * FROM data WHERE Users = '" + textBox1.Text, con);
OleDbDataAdapter ta=new OleDbDataAdapter(cmd);
DataSet ds=new DataSet();
try
{
ta.Fill(ds);
if(ds.Tables[0].Rows.Count==0)
{
MessageBox.Show("User Dos not Exists");
Application.Exit();
}
string hash=ds.Tables[0].Rows[0]["password"].ToString();
string salt=ds.Tables[0].Rows[0]["salt"].ToString();
if(IsPasswordCorrect(textBox2.Text,salt,hash))
{
MessageBox.Show("Success");
this.Hide();
frm2.Show();
}
else
{
MessageBox.Show("Invalid User Name or Password",
"Note",
MessageBoxButtons.OK,
MessageBoxIcon.Exclamation,
MessageBoxDefaultButton.Button1);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
con.Close();
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.