>我写了一个带有输入框的应用程序,我希望人们在其中输入密码,该密码将与存储在我的Web服务器中的文本文件中的密码列表进行比较,该密码是每行中的一个条目,然后可以访问我的应用程序
所以用几句话,我希望将输入框密码逐行与我的文本文件进行比较,但到目前为止我还没有这样做
这是我的代码:
string input =
Microsoft.VisualBasic.Interaction.InputBox("Please enter your password for access to this software", "pass:");
if (input=="")
{
appexit();
}
WebClient client = new WebClient();
Stream stream = client.OpenRead("http://haha.com/access.txt");
StreamReader reader = new StreamReader(stream);
//String content = reader.ReadToEnd();
int counter = 0;
string line;
while ((line = reader.ReadLine()) != null)
{
if (line!=input)
{
MessageBox.Show("This software has been deactivated because of wrong pass", "YOUR ACCESS HAS BEEN LIMITED");
appexit();
}
counter++;
}
reader.Close();
密码文件包含如下行:
hahdfdsf
ha22334rdf
ha2233gg
charlysv-es
错误在哪里?代码将编译,但即使输入了正确的密码,检查也会失败。
根据你的循环,当你得到一行不等于输入时,你就会停止一切——逻辑上是不正确的。您必须比较行,直到其中一行等于输入或文件结尾。
...
bool valid = false;
using (WebClient client = new WebClient())
{
using (Stream stream = client.OpenRead("http://haha.com/access.txt"))
{
using (StreamReader reader = new StreamReader(stream))
{
string line;
while ((line = reader.ReadLine()) != null)
{
if (line.Equals(input))
{
valid = true;
break;
}
}
}
}
}
if (valid)
{
// password is correct
...
}
else
{
MessageBox.Show("This software has been deactivated because of wrong pass", "YOUR ACCESS HAS BEEN LIMITED");
appexit();
}
...