コード例 #1
0
 public DodadiUser(OverviewForm caller)
 {
     this.caller = caller;
     Init();
 }
コード例 #2
0
ファイル: LoginForm.cs プロジェクト: Beta-7/passwordmanager
        private void proceed()
        {
            usernameTB.Text.Trim(); passwordTB.Text.Trim(); confirmPWTB.Text.Trim(); // Remove whitespace from the input entered into the textboxes.
            string databasefajl = Appdatafolder() + "\\" + usernameTB.Text + ".sqlite";

            //TODO: Rework authentication logic
            #region checked
            if (newUserCB.Checked)                                                      //If they selected the register option
            {
                if (passwordTB.Text.Equals(confirmPWTB.Text, StringComparison.Ordinal)) //Check if both of the passwords are the same
                {
                    if (!string.IsNullOrWhiteSpace(usernameTB.Text))                    //Check if the first password field is empty
                    {
                        if (!string.IsNullOrWhiteSpace(passwordTB.Text))                //Check if the second password field is empty
                        {
                            if (!File.Exists(databasefajl))                             //If the file doesn't exist already, as in the username hasn't been registered
                            {
                                sharedSecret = passwordTB.Text;
                                string enkriptirandavid = Cryptography.Encrypt(sharedSecret, passwordTB.Text); //Encrypt the password with itself
                                SQLiteConnection.CreateFile(databasefajl);                                     //Create a db file in %appdata% named username.sqlite
                                SQLiteConnection dbConnection;
                                dbConnection =
                                    new SQLiteConnection("Data Source=" + databasefajl + ";Version=3;");
                                using (var myconnection = new SQLiteConnection(dbConnection))
                                {
                                    myconnection.Open(); //Open the .sqlite file

                                    try
                                    {
                                        string sqlinsert = "insert into passwords (url, name) values ('" + enkriptirandavid + "','" + enkriptirandavid + "');";
                                        //sqlinsert.Parameters.AddWithValue("@url", enkriptirandavid);
                                        SQLiteCommand sqlinsert1 = new SQLiteCommand(sqlinsert, myconnection);
                                        string        komanda    = "create table passwords (id integer primary key autoincrement,URL varchar(150), name varchar(150)" +
                                                                   ",username varchar(150), password varchar(150), notes varchar(1500), visible integer)";
                                        SQLiteCommand izvrsikomanda2 = new SQLiteCommand(komanda, myconnection);
                                        izvrsikomanda2.ExecuteNonQuery(); //Create a table named passwords
                                        sqlinsert1.ExecuteNonQuery();     //Fill the first record's first 2 fields with the encrypted password
                                        myconnection.Close();
                                    }
                                    catch (Exception ex)
                                    {
                                        MessageBox.Show(ex.Message);
                                    }

                                    newUserCB.Checked = false;
                                    MessageBox.Show("Registration was successful"); //Successful registration
                                    proceed();
                                }
                            }
                            else
                            {
                                MessageBox.Show("That your name is already taken."); //Username already exists
                            }
                        }
                        else
                        {
                            MessageBox.Show("A password must be entered."); //No password entered
                        }
                    }
                    else
                    {
                        MessageBox.Show("A username must be entered."); //No username entered
                    }
                }
                else
                {
                    MessageBox.Show("The passwords do not match each other."); //Password mismatch
                }
            }
            #endregion
            else
            {
                if (File.Exists(databasefajl)) //If the file exists already, when the username has been registered
                {
                    sharedSecret = passwordTB.Text;
                    SQLiteConnection dbConnection;
                    dbConnection =
                        new SQLiteConnection("Data Source=" + databasefajl + ";Version=3;");
                    dbConnection.Open(); //Connect to the .sqlite file
                    string           sql     = "SELECT * FROM passwords ORDER BY id ";
                    SQLiteCommand    command = new SQLiteCommand(sql, dbConnection);
                    SQLiteDataReader reader  = command.ExecuteReader(); //Get the records inside
                    while (reader.Read())
                    {                                                   //If (textBox2.Text == Cryptography.Decrypt(reader["URL"].ToString(), sharedsecret))
                        if (Cryptography.Encrypt(passwordTB.Text, sharedSecret) == reader["URL"].ToString())
                        //Encrypt the entered password with the one that is in the first record of the file
                        {
                            username = usernameTB.Text;
                            password = passwordTB.Text;
                            reader.Close();
                            dbConnection.Close();
                            OverviewForm form2 = new OverviewForm(); //Close the SQLITE connection and open the main form.
                            Hide();
                            form2.ShowDialog(this);
                            Close();
                            break;
                        }
                        else
                        {
                            MessageBox.Show("Погрешна лозинка"); //Wrong password
                            reader.Close();
                            dbConnection.Close();
                        }
                    }
                }
                else
                {
                    MessageBox.Show("Корисничкото име не е регистрирано"); //Username not registered
                }
            }
        }