我正在尝试在C#中对我的列表执行验证检查。该列表包含用户名和散列密码。它看起来像:
Shaun,ewoaih3243nfeiwo
John, fewafwea231232
Alex, fhi34325325325
例如Shaun是Username
而ewoaih3243nfeiwo
是password
。我正在函数Read中的数据库中读取此列表。
这是我的代码:
private void LoginButton_Click(object sender, EventArgs e)
{
List<string> List = Read();
label3.Text = null;
textBox2.Text = null;
string Username = textBox1.Text;
string Password = textBox2.Text;
String hashPassword = passHash.HashPass(Password);
for (int i = 0; i < List.Count; i++)
{
if (List[i].Contains(Username))
{
if (List[i].Contains(hashPassword))
{
MessageBox.Show("Welcome, " + textBox1.Text + ". Logging in...", "Welcome");
Form.ActiveForm.Hide();
Main.FrontWindow Start = new Main.FrontWindow();
Start.ShowDialog();
}
else
{
label3.Text = "Username and password do not match.";
}
}
else
{
label3.Text = "User does not exist";
}
}
}
然而,当我填写文本框并运行时,我总是得到用户不存在的结果,即使它非常清楚。例如,当我输入Shaun和一个不正确的密码时,它应该说用户名和密码不匹配,而不是用户不存在。
编辑:这是我创建字典的方式
public Dictionary<string, string> Read()
{
string username;
string password;
string ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=F:\A2 Computing\C# Programming Project\Database1.accdb";
string SelectQuery = "SELECT Username, Password FROM users";
OleDbConnection Connection = new OleDbConnection(ConnectionString);
OleDbCommand Command = new OleDbCommand(SelectQuery, Connection);
Command.Connection.Open();
OleDbDataReader Reader = Command.ExecuteReader(CommandBehavior.CloseConnection);
List<string> usernameList = new List<string>();
List<string> passwordList = new List<string>();
while (Reader.Read())
{
username = (string)Reader["Username"];
string usernameAdd = Convert.ToString(username);
usernameList.Add(usernameAdd);
password = (string)Reader["Password"];
string passwordAdd = Convert.ToString(password);
passwordList.Add(passwordAdd);
}
var userDictionary = usernameList.Zip(passwordList, (u, p) => new { u, p })
.ToDictionary(x => x.u, x => x.p);
var userList = userDictionary.ToList();
listBox1.DataSource = userList;
return userDictionary;
}
答案 0 :(得分:3)
您应该真正分开您的用户名和密码。
请使用Dictionary<string, string>
。
另外,不要使用for循环,然后检查它是Contains()
项目。那太荒谬了!
您可以执行以下操作:
Dictionary<string, string> namesPasswords = Read();
// ... hashing, etc.
if(namesPasswords.Keys.Contains(Username))
if(namesPasswords[Username].Equals(hashPassword))
// Welcome...
else
// Error...
答案 1 :(得分:0)
您所做的事情不是首选,您应该使用Dictionary<string, string>
或List<KeyValuePair<string, string>>
来保存您的用户名和密码。
尽管您所做的事情仍然不是首选,但您仍可以添加指定的断点以使其正常工作。这个想法是,如果第一个IF
找到一个用户名,那么循环不应该继续,而应该打破它应该做的事情。
for (int i = 0; i < List.Count; i++)
{
if (List[i].Contains(Username))
{
if (List[i].Contains(hashPassword))
{
MessageBox.Show(
"Welcome,
" + textBox1.Text + ". Logging in...", "Welcome");
Form.ActiveForm.Hide();
Main.FrontWindow Start = new Main.FrontWindow();
Start.ShowDialog();
}
else
{
label3.Text = "Username and password do not match.";
}
break;
}
else
{
label3.Text = "User does not exist";
}
}
或者你也可以这样做(根本不认为)。
注意我只是在这里编写此代码,请检查编译错误。
var user = List
.Select(e=>e.Split(",", StringSplitOptions.RemoveEmpty).Select(a=>
new
{
Username = a[0].Trim(),
Password=a[1].Trim()
}))
.Where(o=>o.Username = Username /*username entered by the user*/)
.SingleOrDefault();
if(user!=null)
{
if(user.Password == Password /*password entered by the user*/) {/*Valid User*/}
else{/*Invalid password OR Username*/}
}
else {/*User does not exist*/}