C# Q:如何在C中使用StreamReader创建登录#
因此,我正在尝试创建一个登录系统,通过使用StreamReader从文本文件中获取必要的信息(名称和密码) 我的代码:C# Q:如何在C中使用StreamReader创建登录#,c#,login,streamreader,C#,Login,Streamreader,因此,我正在尝试创建一个登录系统,通过使用StreamReader从文本文件中获取必要的信息(名称和密码) 我的代码: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; usi
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace PizzaOrder
{
public partial class Login : Form
{
public int attempts = 0;
public bool success = false;
public bool usernameSuccess = false;
public bool passwordSuccess = false;
public static string LogIn = @"C:\Users\P110095127\Documents\Visual Studio 2015\Projects\PizzaOrder\PizzaOrder\userDetails.txt";
public Login()
{
InitializeComponent();
}
private void LoginButton_Click(object sender, EventArgs e)
{
string user = "";
string passcode = "";
int counter = 0;
user = usernameBox.Text;
passcode = passwordBox.Text;
using (StreamReader readFile = new StreamReader(LogIn))
{
counter = counter + 1;
string line;
while ((line = readFile.ReadLine()) != null)
{
var parts = line.Split(',');
user = parts[0];
passcode = parts[1];
attempts = Convert.ToInt32(parts[3]) + 1;
if (user == parts[1])
{
usernameSuccess = true;
}
else
{
MessageBox.Show("The username is incorrect");
attempts++;
break;
}
if (passcode == parts[2])
{
passwordSuccess = true;
}
else
{
MessageBox.Show("The password is incorrect");
attempts++;
break;
}
if (attempts == 3)
{
MessageBox.Show("The account has been blocked!");
this.Hide();
}
if (usernameSuccess && passwordSuccess == true)
{
this.Hide();
Order Order = new Order();
Order.Show();
}
}
}
}
我希望代码首先检查用户名是否正确,如果正确,然后继续输入密码,否则我希望显示用户名输入不正确。密码也是一样。我已经创建了一个名为'attempts'的变量,我希望每次尝试的用户名或密码出错时,尝试次数都增加1。如果尝试次数达到3次,则该帐户将被阻止
每次我输入正确的用户名和密码时,仍然会收到错误消息“用户名不正确”。另外,在3次失败的尝试之后,它仍然没有隐藏窗口,而是不断地说用户名不正确。我认为您没有正确地实现if语句
user = parts[0];
passcode = parts[1];
您正在用户中存储0索引中的数据。和一个指向密码的索引
但是,在ifs中,您将用户与部件[1]进行比较?这段代码非常混乱。另外,请发布txt文件的内容,以便我们可以查看parts变量中存储的内容
编辑:如果有任何问题,请执行以下操作:
if (usernameBox.Text == parts[0])
if (passwordBox.Text == parts[1])
user = parts[0];
passcode = parts[1];
if (usernameBox.Text == parts[0])
if (passwordBox.Text == parts[1])
attempts = Convert.ToInt32(parts[3]) + 1;
应该是:
尝试次数=转换为32(部分[2])+1 您采取的方法要求用户名和密码位于您通过索引获取的确切位置。此外,如果您在该行上有任何其他文本,则会妨碍您登录 您需要清理文档中的数据。例如:
private static IEnumerable<string> ReadByLine(string path)
{
string line;
using (StreamReader reader = File.OpenText(path))
while ((line = reader.ReadLine()) != null)
yield return line;
}
以上内容将遍历您的文档,搜索用户名和密码。为了确保它的存在,现在您可以实际查看用户名和密码是否被找到,并且可以正确地确保它们匹配
如上所述,这是一种可怕的安全方法。此外,您的问题源于按索引分配的变量选择不正确
重要提示:Ceasar已经编写了一个直接修复程序,这只是为了强调数据清理的重要性,尤其是在文档中。您可以通过不进行数据清理来引入各种问题 为什么不使用
File.ReadAllText()
或File.ReadAllLines()
user=parts[0]代码>密码=部件[1]代码>if(user==parts[1])
这似乎有问题。您正在覆盖您的用户输入。我也希望这是一个学校的项目或东西。这是一种处理安全性的糟糕方法。@Blast\u dan是的,我只是想学习如何使用文本文件系统。你逐行阅读的方式比使用file.ReadAllText()
或file.ReadAllLines()
更好,所以我会保留这些代码。我推荐你,所有分配的数组索引都很难跟踪。