-1

好的,感谢 LordALMMa,我把它整理好了,但现在我遇到了另一个问题。我想确定用户在注册时是否单击 Admin 或 User 单选按钮。我想我应该将它附加到名称和密码所在的文本文件的行尾,但我该怎么做呢?以下是相关代码:

单选按钮检查

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 从单选按钮方法传递给 SW。我该怎么做,因为我已经传递了一个布尔值?

安东尼

4

4 回答 4

2

问题的一部分是您没有编写与您正在阅读的字符串相同的字符串:

writer.WriteLine("Password: " + userName + " " + "Password: " + password);

我猜这是您帖子中的错字……但如果不是,那可能是您的问题。

另一个问题可能就是这里:

using (StreamWriter writer = new StreamWriter(path, true))

如果您查看有关 StreamWriter 构造函数重载的文档,您会看到您指定了append = true. 您将每组登录凭据附加到单独一行的文件中。但后来,您只读取该文件的第一行。因此,您将始终阅读首次创建文件时输入的第一组凭据。

除此之外,我希望您只是将其作为一个实验,因为将密码写入这样的文件并不是一种安全的管理密码的方法。此外,如果将 Stream 包装在一个块中,则不需要在 Stream 上调用 Close 和 Dispose using,因此您应该坚持这样做。

于 2013-09-30T18:50:49.697 回答
0

你检查过你的输出文件吗?您正在写密码:X 密码:Y

writer.WriteLine("Password: " + userName + " " + "Password: " + password);

并且您正在检查用户名:X 密码:Y

if (user == ("Username: "+userNameBox.Text.Trim()+" "+"Password: "+passwordBox.Text.Trim()))
于 2013-09-30T18:50:50.337 回答
0

您正在将行添加为

writer.WriteLine("Password: " + userName + " " + "Password: " + password);
                  ^1                              ^2

^1一定是Username:

有一些要点我不能不指出就通过:

  1. 如果文件结构损坏,你会怎么做?

  2. 如果用户想使用相同的用户名和密码注册两次怎么办?

  3. 请对密码进行编码。这不是道德。您将在其他地方使用相同帐户信息的成员置于危险之中。

  4. 尝试使用比文本文件更强大、更快的数据库。

于 2013-09-30T18:51:19.427 回答
0

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 的复杂性,还因为为了使其正常工作,我必须在录制之前检查它们是否退出,因此更改注册方法。

但我想你已经明白了。

于 2013-09-30T18:57:56.540 回答