public static void LoadUserFile() { Regex regex = new Regex(@"^<Username>(.*)</Username><Passwords>(.*)</Passwords>$"); Regex passregex = new Regex(@"<Password>(.*?)</Password>"); userTable.Clear(); User u; string username; List<string> passwords; string line; try { string path = String.Format("{0}\\{1}", Application.StartupPath, fileName); FileStream fs = new FileStream(path, FileMode.Open); StreamReader sr = new StreamReader(fs); while (!sr.EndOfStream) { passwords = new List<string>(); line = sr.ReadLine(); Match m = regex.Match(line); if (m.Success) { username = m.Groups[1].Value; string s = m.Groups[2].Value; foreach (Match m2 in passregex.Matches(s)) { passwords.Add(m2.Groups[1].Value); } } else { throw new Exception(String.Format("Password file: \"{0}\" invalid",fileName)); } //string[] values = line.Split(' '); //username = sr.ReadLine(); //password = sr.ReadLine(); u = new User(username); u.passwords = passwords; userTable[u.Username] = u; } sr.Close(); fs.Close(); } catch (System.IO.FileNotFoundException) { } }
private void NewUser() { UserDialogue userDialog = new UserDialogue(); userDialog.Text = "New User"; userDialog.userTextBox.Text = ""; userDialog.userTextBox.Enabled = true; userDialog.passwordTextBox.Text = ""; userDialog.ShowDialog(); if (userDialog.DialogResult == DialogResult.OK) { if (userDialog.userTextBox.Text != "") { bool add = true; foreach (User u in userList.Items) { if (userDialog.userTextBox.Text == u.Username) { MessageBox.Show("Username: \"" + u.Username + "\" already taken."); add = false; break; } } if (add) { User u2 = new User(userDialog.userTextBox.Text); if (ServerOptions.UseStrongPasswords) { if (Authenticator.IsStrongPassword(u2, userDialog.passwordTextBox.Text)) { u2.Password = PasswordHashUtility.HashPassword(userDialog.passwordTextBox.Text); userList.Items.Add(u2); } else { MessageBox.Show(passwordRulesString); } } else { u2.Password = PasswordHashUtility.HashPassword(userDialog.passwordTextBox.Text); userList.Items.Add(u2); } } } else { MessageBox.Show("Invalid Username"); } } SetButtons(); }
public static bool IsStrongPassword(User user, string password) { // 2+ punctuation Regex punctuationRegex2 = new Regex(@"\p{P}"); MatchCollection c; c = punctuationRegex2.Matches(password); if (c.Count < 2) { return false; } // Minimum of 10 characters if (password.Length < 10) { return false; } // 2+ numbers Regex numbersRegex = new Regex(@"[0-9]"); c = numbersRegex.Matches(password); if (c.Count < 2) { return false; } // 2+ uppercase letters Regex uppercaseRegex = new Regex(@"[A-Z]"); c = uppercaseRegex.Matches(password); if (c.Count < 2) { return false; } // 2+ lowercase letters Regex lowercaseRegex = new Regex(@"[a-z]"); c = lowercaseRegex.Matches(password); if (c.Count < 2) { return false; } // 2+ punctuation Regex punctuationRegex = new Regex(@"\p{P}"); c = punctuationRegex.Matches(password); if (c.Count < 2) { return false; } // Cannot re-use the last 10 passwords if (user.passwords.Contains(PasswordHashUtility.HashPassword(password))) { return false; } return true; }