C# 错误的登录系统,将不允许登录
好的,多亏了洛达尔玛,我把它整理好了,但现在我有另一个问题。我想确定用户在注册时是单击Admin还是user radiobutton。我想我应该把它附加到文本文件中名称和密码所在的行的末尾,但是我该怎么做呢?以下是相关代码: 单选按钮检查C# 错误的登录系统,将不允许登录,c#,.net,winforms,C#,.net,Winforms,好的,多亏了洛达尔玛,我把它整理好了,但现在我有另一个问题。我想确定用户在注册时是单击Admin还是user radiobutton。我想我应该把它附加到文本文件中名称和密码所在的行的末尾,但是我该怎么做呢?以下是相关代码: 单选按钮检查 public bool radioButtons() { string usertypebutton; if (!userButton.Checked && !adminButton.Checked)
public bool radioButtons()
{
string usertypebutton;
if (!userButton.Checked && !adminButton.Checked)
{
MessageBox.Show("You must select an account type");
return false;
}
else
{
if (userButton.Checked)
{
usertypebutton = "User";
}
else
{
usertypebutton = "Admin";
}
return true;
}
}
用于注册的Streamwriter:
public void mySW()
{
string path = @"C:\Other\myFile.txt";
string userName = userNameBox.Text;
string password = passwordBox.Text;
string usertype = usertypebutton;
using (StreamWriter writer = new StreamWriter(path, true))
{
writer.WriteLine("Username: {0} Password: {1} Type: {3}" , userName, password, usertype);
// No need to close nor dispose your StreamWriter.
// You're inside a using statement for that!
}
MessageBox.Show("Thanks for registering! \n\nYou may now log in!", "Registration SuccessFul");
Application.OpenForms[0].Show();
this.Close();
}
登录:
private void logonButton_Click(object sender, EventArgs e)
{
// Loads your users storage
var users = File.ReadAllLines(@"C:\Other\myFile.txt");
// Creates the line with username + password
var usernamePassword = String.Format("Username: {0} Password: {1}", userNameBox.Text, passwordBox.Text);
// Locates the user on your storage
var userFound = users.SingleOrDefault(_u => _u.Equals(usernamePassword));
if (userFound != null)
{
MessageBox.Show("Welcome back, " + userNameBox.Text);
}
else
{
MessageBox.Show("Sorry, you have entered incorrect details\n\nPlease try again");
userNameBox.Text = "";
passwordBox.Text = "";
}
}
所以(我认为)本质上我想把值usertypebutton从radiobutton方法传递给SW。我应该怎么做,因为我已经在传递一个布尔值了
Anthony问题的一部分是,您所写的字符串与所读的字符串不同:
writer.WriteLine(“密码:+userName+”+“密码:+Password”)代码>
我猜那是你帖子中的一个打字错误。。。但如果不是,那可能是你的问题
另一个问题可能就在这里:
使用(StreamWriter writer=newstreamwriter(path,true))
如果您查找StreamWriter构造函数重载的文档,就会看到您指定了append=true
。您正在将每组登录凭据附加到其自己行上的文件中。但是稍后,您只读取该文件的第一行。因此,您将始终读取首次创建文件时输入的第一组凭据
除此之外,我希望您只是在做一个实验,因为将密码写入这样的文件并不是一种安全的管理密码的方法。另外,如果使用
块将流包装在中,则不需要调用Close和Dispose,因此您应该坚持这样做。检查输出文件了吗?您正在写入密码:X密码:Y:
您正在检查用户名:X密码:Y
您正在将行添加为
writer.WriteLine("Password: " + userName + " " + "Password: " + password);
^1 ^2
^1
必须是用户名:
有几点我必须指出才能通过:
如果文件结构损坏,您会怎么做
如果用户希望使用相同的用户名和密码注册两次,该怎么办
请对密码进行编码。这不是道德。您将在其他地方使用相同帐户信息的会员置于风险之中
尝试使用比文本文件更强更快的数据库
Anthony,尽管以这种方式存储登录是一个重大的安全问题(这甚至不再是一个风险),但我会对您的代码进行一些更改
问题是您没有存储“用户名:[用户名]密码:[密码]”。
如果您再次检查您的保存方法,您将存储“密码:[用户名]密码:[密码]”。这就是为什么他们从未被发现
以下是一些变化:
考虑:
public void mySW()
{
string path = @"C:\Other\myFile.txt";
string userName = userNameBox.Text;
string password = passwordBox.Text;
using (StreamWriter writer = new StreamWriter(path, true))
{
// This overload makes your life easier by auto-formatting variables for you.
// Also, avoid the "string1 + string2" concatenation mode.
// Use String.Format instead. It's easier to read and keep over time.
writer.WriteLine("Username: {0} Password: {1}", userName, password);
// No need to close nor dispose your StreamWriter.
// You're inside a using statement for that!
}
MessageBox.Show("Thanks for registering! \n\nYou may now log in!", "Registration SuccessFul");
Application.OpenForms[0].Show();
this.Close();
}
您的其他方法应该如下所示:
{
// Loads your users storage
var users = File.ReadAllLines(@"C:\Other\myFile.txt");
// Creates the line with username + password
var usernamePassword = String.Format("Username: {0} Password: {1}", userNameBox.Text, passwordBox.Text);
// Locates the user on your storage
// This uses Linq syntax with lambda. Linq without lamba looks similar to SQL.
// Lambda is a bit more advanced but reduces code-size and it's easier to understand (IMHO).
// This code will iterate through users (list of string) and try to retrieve one that's equal to the contents of usernamePassword.
var userFound = users.SingleOrDefault(_u => _u.Equals(usernamePassword));
// If null, indicates that no username/password combination was found.
if (userFound != null)
{
MessageBox.Show("Welcome back, " + userNameBox.Text);
}
else
{
MessageBox.Show("Sorry, you have entered incorrect details\n\nPlease try again");
userNameBox.Text = "";
passwordBox.Text = "";
}
}
我没有检查例外情况。如果找到与搜索模式匹配的2条或多条记录,SingleOrDefault将引发异常
我没有检查它,因为这会增加try-catch的复杂性,也因为为了让它正常工作,我必须在录制之前检查它们是否存在,所以要更改register方法
但我认为您已经有了这个想法。我希望这只是一个例子,您并不是真的将登录信息存储在文本文件中。您的注册页面将“密码:”写入文件两次。也就是说,请不要这样做。将密码以明文形式写入磁盘上的文件是一种非常糟糕的做法,因为大多数用户会在许多地方重复使用标准密码(即使他们确实不应该重复使用)。如果您确实需要支持身份验证,请花时间研究并理解它。您需要在visual studio中逐步编写代码。在需要修复的情况下失败的比较上放置断点!什么是用户?您要将其与之进行比较的字符串是什么?为什么不同?文件中有多少用户?您只读取文件中的第一行并进行检查。此外,我同意Shoe的观点,这是一个例子;或者你正在做的帮助学习的事情:)对不起,是的,我注意到当我发布它时,它立即更改为用户名,但仍然失败。我想是因为它只是读了第一行。那么,我如何设置它来阅读所有的文字,而不仅仅是第一行?我理解其中的含义,但它只是我老板的一个本地软件,并不是为了任何安全,它只是为了确定谁在使用该软件,仅此而已。我知道我需要学习安全,但这就足够了。var userFound=users.SingleOrDefault(_=>_.Equals(usernamePassword))是什么;意思?它是一个linq表达式,用于在集合(用户
)中搜索等于变量的记录。如果发现两个或多个异常,将引发异常。如果没有找到,它将返回null(这就是我检查null的原因)。如果找到一个,它已经为您检索。
public void mySW()
{
string path = @"C:\Other\myFile.txt";
string userName = userNameBox.Text;
string password = passwordBox.Text;
using (StreamWriter writer = new StreamWriter(path, true))
{
// This overload makes your life easier by auto-formatting variables for you.
// Also, avoid the "string1 + string2" concatenation mode.
// Use String.Format instead. It's easier to read and keep over time.
writer.WriteLine("Username: {0} Password: {1}", userName, password);
// No need to close nor dispose your StreamWriter.
// You're inside a using statement for that!
}
MessageBox.Show("Thanks for registering! \n\nYou may now log in!", "Registration SuccessFul");
Application.OpenForms[0].Show();
this.Close();
}
{
// Loads your users storage
var users = File.ReadAllLines(@"C:\Other\myFile.txt");
// Creates the line with username + password
var usernamePassword = String.Format("Username: {0} Password: {1}", userNameBox.Text, passwordBox.Text);
// Locates the user on your storage
// This uses Linq syntax with lambda. Linq without lamba looks similar to SQL.
// Lambda is a bit more advanced but reduces code-size and it's easier to understand (IMHO).
// This code will iterate through users (list of string) and try to retrieve one that's equal to the contents of usernamePassword.
var userFound = users.SingleOrDefault(_u => _u.Equals(usernamePassword));
// If null, indicates that no username/password combination was found.
if (userFound != null)
{
MessageBox.Show("Welcome back, " + userNameBox.Text);
}
else
{
MessageBox.Show("Sorry, you have entered incorrect details\n\nPlease try again");
userNameBox.Text = "";
passwordBox.Text = "";
}
}