即使Log.txt文件包含我尝试登录的用户名和密码,以下STILL也无法正常工作.
这是我的REMADE登录代码:
public void button1_Click(object sender, EventArgs e)
{
bool loggedin = false;
Form2 form2 = new Form2();
Form1 form1 = new Form1();
var contents = File.ReadAllLines(@"C:\log.txt");
if (contents.Contains(Username.Text))
{
loggedin = true;
MessageBox.Show("Login sucessfully", "Program", MessageBoxButtons.OK, MessageBoxIcon.Information);
form1.Hide();
form2.Show();
}
else
{
if (loggedin == false)
{
MessageBox.Show("Invalid username or password.", "Program", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的注册码:
{
using (System.IO.StreamWriter writer = new System.IO.StreamWriter("C:\\log.txt", true))
{
writer.WriteLine("\r" + "Username : " + textBox1.Text);
writer.WriteLine("Password : " + textBox2.Text);
writer.WriteLine("Age : " + textBox4.Text);
}
Form5 form5 = new Form5();
Form1 form1 = new Form1();
form1.Show();
this.Hide();
}
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
您的代码无法正常工作,因为只是查看字符串"log.txt"是否包含用户名实际上是否打开文件并检查内容.
没有必要使用StreamReader/ StreamWriter来读取文件的文本.您只需要打开文件,阅读内容,然后检查以下内容:
contents = File.ReadAllLines(@"C:\Path\To\File\log.txt");
if(contents.Contains(Username.Text))
// ...
Run Code Online (Sandbox Code Playgroud)我真诚地希望你不要指望这个系统安全性最低.将所有用户名/密码组合存储在纯文本文件中是非常糟糕的.任何人都可以打开它,阅读它,并查看文件中的每个组合.此外,我所要做的就是猜测文件中的一些随机字符串.我最好的选择是猜一封信.我将有1/26的机会在你的文件中存在该角色,我将被授予访问权限.