简体   繁体   English

读取和写入文件以进行身份​​验证

[英]Reading and writing to a file for authentication

I have been trying to make a simple authentication system in c#.我一直在尝试用 c# 制作一个简单的身份验证系统。 I am trying to make it read a text file and if it has the correct answer/username, continue.我试图让它读取一个文本文件,如果它有正确的答案/用户名,请继续。 If it doesn't it has a textbox.Show on it.如果它没有它有一个文本框。显示它。 My current code for this is:我目前的代码是:

if(textBox1.Text == "wlkey1")
{
    Hide();
    string fileName1 ="authentication.txt";
    System.IO.File.WriteAllText(fileName1, textBox1.Text);
}
else{
    MessageBox.Show("Yikes. That's incorrect.", "Uh oh.");
}

You are Writing to file instead of reading it.您正在写入文件而不是读取文件。 you must use ReadAllText()你必须使用ReadAllText()

string fileName1 ="authentication.txt";
string curPass = "someDefaultPass";
if(System.IO.File.Exists(fileName1)
    curPass = System.IO.File.ReadAllText(fileName1);
if(textBox1.Text == curPass)
{
    Hide();
}
else{
    MessageBox.Show("Yikes. That's incorrect.", "Uh oh.");
}

By the way, as you may already know, saving passwords on a file is not secure.顺便说一下,您可能已经知道,将密码保存在文件中是不安全的。

for writing well you have already done it:为了写得好,你已经做到了:

string fileName1 ="authentication.txt";
System.IO.File.WriteAllText(fileName1, textBox1.Text);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM