Pergunta

Got an issue here. I have a program that when it starts a loginForm dialog pops up. Now on this loginForm I've added a rememberMe (onthoudMij) button. So if the user selects this box log's in, closes the program and then opens it again, his user credentials will already be filled in.

Here's my code for my loginButton.

private void loginButton_Click(object sender, EventArgs e)
{
    try
    {
        var sr = new System.IO.StreamReader("C:\\" + inlogNaamTextbox.Text + "\\Login.txt");
        gebruikersnaam = sr.ReadLine();
        passwoord = sr.ReadLine();
        sr.Close();

        if (gebruikersnaam == inlogNaamTextbox.Text && passwoord == inlogPasswoordTextbox.Text)
        {
            classUsername.name = inlogNaamTextbox.Text;

            MessageBox.Show("Je bent nu ingelogd!", "Succes!");
            this.Dispose();
        }
        else
        {
            MessageBox.Show("Gebruikersnaam of wachtwoord fout!", "Fout!");
        }
    }
    catch (System.IO.DirectoryNotFoundException ex)
    {
        MessageBox.Show("De gebruiker bestaat niet!", "Fout!");
    }
}

NOW MY PROBLEM IS:

I have searched the web for options on remember me implementation, but they always use SQL databases etc. I just have a login that stores the username & password in a txt file as you can see in the code.

Question is..

So no my question is; How can I implement this remember me checkbox? I already know I should use System.Net , but what else for this to work without sql databases or anything like that?

Foi útil?

Solução

If your problem is the saving part, you could use something like 3DES encryption to have a somewhat save solution to save the username and password.

You could use your current code and add something like in this SO article to encrypt and decrypt the password: How to implement Triple DES in C# (complete example)

If the problem is just how to do read/save this?

I would create a text, something like this:

private void CallOnFormLoad()
{
    string[] allLines = File.ReadAllLines(@"C:\passwordfile.txt");

    if (allLines.Length > 1)
    {
        // at least one 2 lines:
        inlogNaamTextbox.Text = allLines[0];
        inlogPasswoordTextbox.Text = allLines[1];
    }
}

private void loginButton_Click(object sender, EventArgs e)
{
    try
    {
        var sr = new System.IO.StreamReader("C:\\" + inlogNaamTextbox.Text + "\\Login.txt");
        gebruikersnaam = sr.ReadLine();
        passwoord = sr.ReadLine();
        sr.Close();

        if (gebruikersnaam == inlogNaamTextbox.Text && passwoord == inlogPasswoordTextbox.Text)
        {
            classUsername.name = inlogNaamTextbox.Text;

            MessageBox.Show("Je bent nu ingelogd!", "Succes!");

            File.WriteAllLines(@"C:\passwordfile.txt", string.Format("{0}\n{1}", inlogNaamTextbox.Text, inlogPasswoordTextbox.Text))

            // Don't call Dispose!
            // this.Dispose();
        }
        else
        {
            MessageBox.Show("Gebruikersnaam of wachtwoord fout!", "Fout!");
        }
    }
    catch (System.IO.DirectoryNotFoundException ex)
    {
        MessageBox.Show("De gebruiker bestaat niet!", "Fout!");
    }
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top