C# 登录表单C中的区分大小写#
我的登录表单正在运行,但是当我输入的用户名在单词的第一个字母中使用大写字母,并且数据库中的用户名都是小写字母时,它仍然允许访问,并且登录成功 你能帮我修一下吗? 我真的需要你的帮助 这是我当前的代码C# 登录表单C中的区分大小写#,c#,ms-access,C#,Ms Access,我的登录表单正在运行,但是当我输入的用户名在单词的第一个字母中使用大写字母,并且数据库中的用户名都是小写字母时,它仍然允许访问,并且登录成功 你能帮我修一下吗? 我真的需要你的帮助 这是我当前的代码 private void button1_Click(object sender, EventArgs e) { if (textBox1.Text == "" || textBox2.Text == "")
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
子句
string SqlString=“更新数据集Player1=?,Player2=?其中
用户名=@user”
如果没有它,更新将应用于表中的所有行。请注意,这与DELETE语句的工作原理完全相同
现在,您通常不会在这样的用户名上进行匹配,您将使用tables主键进行此类比较。除此之外,这是SQL101。我强烈建议在继续下一步之前,先读一本RDBMS书籍,学习SQL的基础知识。我可以说,要在db中保存密码,必须使用哈希
算法保存密码数据,例如MD5
或SHA1
当用户为登录输入密码时,您将散列为密码输入的字符串,并将此字符串与保存在db中的密码进行比较
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分配一个salt,并为每个用户将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();
}
}
}
在您的SQL中添加一个WHERE
子句,该子句限制对登录用户的更改。您能提供一个示例吗,先生?@Mehdi Haghshena,如果您介意的话,先生,我只想问一下如何在上面的代码中使用它?先生,你能提供吗?事实上,这对我来说太复杂了,我只是c#的新手。请检查我更改了你的代码,但没有测试它。你可能会被迫调试它,先生,请小心,这里有一个错误string hash=ds.Tables[0]。Rows[0][“password”];字符串salt=ds.Tables[0]。行[0][“salt”];它说,无法将类型“object”隐式转换为“string”。存在显式转换(是否缺少转换?),先生,我该怎么办?string hash=ds.Tables[0]。Rows[0][“password”].ToString();