-1

わかりました、LordALMMa のおかげで整理できましたが、別の問題が発生しました。登録時にユーザーが [管理者] または [ユーザー] ラジオボタンをクリックしたかどうかを判断したい。名前とパスワードがあるテキストファイルの行末に追加する必要があると思いますが、どうすればよいですか? 関連するコードは次のとおりです。

ラジオボタンチェック

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;

        }
    }

登録するストリームライター:

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 = "";
        }
    }

だから(私が思うに)本質的に、radiobuttonメソッドから値usertypebuttonをSWに渡したいと思います。すでにブール値を渡しているので、どうすればよいですか?

アンソニー

4

4 に答える 4

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 に答える