0

Im making a login application in C# and i want to save email, username and password in a single .txt file, how can i do this and then load the username and password when im loggin in and the email in the main window?

this is my login code:

string dir = textBox1.Text;
            if (!Directory.Exists("data\\" + dir))
                MessageBox.Show("User Dosen't Exist!", dir);
            else
            {
                var sr = new StreamReader("data\\" + dir + "\\data.ls");

                string encusr = sr.ReadLine();
                string encpass = sr.ReadLine();
                string encemail = sr.ReadLine();
                sr.Close();

                string decusr = Encryption.Encrypt.decrypt(encusr);
                string decpass = Encryption.Encrypt.decrypt(encpass);
                string decemail = Encryption.Encrypt.decrypt(encemail);

                if (decusr == textBox1.Text && decpass == textBox2.Text)
                {

                    MessageBox.Show("Welcome To Private Area", decusr);
                    Main_Form frm = new Main_Form();
                    frm.Show();
                    this.Hide();
                }
                else
                {
                    MessageBox.Show("Password Or Username Is Wrong!", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }

and this the save code:

string dir = textBox1.Text;
            Directory.CreateDirectory("Data\\" + dir);

            var sw = new StreamWriter("Data\\" + dir + "\\data.ls");

            string encusr = Encryption.Encrypt.encrypt(textBox1.Text);
            string encpass = Encryption.Encrypt.encrypt(textBox2.Text);
            string encemail = Encryption.Encrypt.encrypt(textBox3.Text);

            sw.WriteLine(encusr);
            sw.WriteLine(encpass);
            sw.WriteLine(encemail);
            sw.Close();

            MessageBox.Show("User Was Successfully Created: " + textBox1.Text);
            this.Close();
MoDz Trolls
  • 37
  • 2
  • 5
  • Do you experience any errors with the implementation? – Dmitry Bychenko Dec 02 '16 at 12:20
  • IMO you should make it in CSV like style ("username";"password";"email"), and then you can read this file line by line and check if (line[0] == usernameInput) – mrogal.ski Dec 02 '16 at 12:24
  • Sure you are going to find the way to fix it, but I think that even when you find a solution you are going in the wrong way. txt files are not the best way to store that information. Try to look for XML files or JSON files, and if you want to go deeper, why dont you try with SQLLite? – Facundo La Rocca Dec 02 '16 at 12:38
  • You should find out what is serialization. Write a class with that fields, serialize it to JSON or XML, it is much easier. Example: http://stackoverflow.com/a/4123648/4697963 – Markiian Benovskyi Dec 02 '16 at 12:48

2 Answers2

0

You could change the Main_Form to include an email field like so:

public class Main_Form : Window
{
    public string Email { get; set; }
    // skipped rest of main form implementation
}

Then you could set the email field when creating the Main_Form:

MessageBox.Show("Welcome To Private Area", decusr); Main_Form frm = new Main_Form(); frm.Email = decemail; frm.Show(); this.Hide();

SynerCoder
  • 12,493
  • 4
  • 47
  • 78
0

First of all your code will just look for the first entry inside the file, this you should fix by doing something like :

using (var sr = new StreamReader("data\\" + dir + "\\data.ls")){
    string line = string.Empty;
    while((line = sr.ReadLine()) != null) {
        // assume that 1 is username
        if(decusr == textBox1.Text) {
            if( (line = sr.ReadLine()) == decpass == textBox2.Text) {
                // user is logged in store email
                UserEmail = sr.ReadLine();
            }
        }
    }
}

After doing this you can just add member field and property into your Main_Form like so:

public class Main_Form : Window
{
    public string UserEmail { get; private set; }
    /*
     * Rest of your code
     */
}

EDIT: (the better way but still using txt file)

Better way would be to just rewrite your save/load logic to store data in CSV like file:

public string ProduceLine(string username, string password, string email){
    return string.Join(";", new string[] { 
        Encryption.Encrypt.encrypt(username),
        Encryption.Encrypt.encrypt(password),
        Encryption.Encrypt.encrypt(email)
    }; // this will return something like "encrypted_username;encrypted_password;encrypted_email"
}

Then when you will load the file you can just use simple method to compare:

public string TryLogin(string username, string password) {
    // this will return user email upon succesfull logging in
    using (var sr = new StreamReader("data\\" + dir + "\\data.ls")){
        string line = string.Empty;
        while((line = sr.ReadLine()) != null) {
            string[] extracted = string.Split(";");
            if(extracted[0] == Encryption.Encrypt.encrypt(username) && extracted[1] == Encryption.Encrypt.encrypt(password)) {
                return Encryption.Encrypt.decrypt(extracted[1]);
            }
        }
    }
    return string.Empty;
}

This will allow you to trim down your methods like:

string email = TryLogin(textBox1.Text, textBox2.Text);
if(!string.IsNullOrEmpty(email))
{
    MessageBox.Show("Welcome To Private Area", decusr);
    Main_Form frm = new Main_Form();
    frm.Show();
    this.Hide();
}
else
{
    MessageBox.Show("Password Or Username Is Wrong!", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
}    
mrogal.ski
  • 5,828
  • 1
  • 21
  • 30