0

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?

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
RawMeat
  • 1
  • 4
  • What is problem with your current code? It seems to me the question is totally unrelated to your code. – Patrick Hofman May 09 '14 at 08:49
  • No the current code is fine and works perfectly. I just wanted to illustrate that I don't use any sql databases to store my login details. I just write them to txt files. And I now want to know how to implement a remember me checkbox on the loginform. I already have the checkbox on my form but it has no code in it yet since I have no idea how to do this. – RawMeat May 09 '14 at 08:51

1 Answers1

0

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!");
    }
}
Community
  • 1
  • 1
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
  • I don't need to encrypt the password. This is just a project where everyone may see the user credentials. So the security doesn't matter. I only want to have the user credentials remembered the next time the program starts from let's say the same PC. So I'm guesssing the code would start with if (checkbox = checked) ... Something like that – RawMeat May 09 '14 at 08:54
  • @RawMeat: See the update. Call the method `CallOnFormLoad` in the constructor of your form. – Patrick Hofman May 09 '14 at 08:59
  • Damn i thought it worked but it doesn't work anymore now. My visual studio says the name "file" does not exist. In what file or name should I change it ? Thanks in advance – RawMeat May 09 '14 at 11:02
  • @RawMeat: Did you create the file passwordfile.txt? Maybe you need to change it to a location where you can write in. – Patrick Hofman May 09 '14 at 11:26
  • No but I mean the error that is given is that the "file".WriteAllLines (so that word file) doesn't exist? – RawMeat May 09 '14 at 12:54
  • It is `File`, not `file` (note the capital `F`). It is a reference to the `System.IO.File` class. – Patrick Hofman May 09 '14 at 12:56
  • I know I've named it File I've even added using System.IO.File in my login form and still same error ;/ – RawMeat May 09 '14 at 13:27