protected void PasswordGenBtn_Click(object sender, EventArgs e) { var passwords = ""; PasswordOptions pwOpts = new PasswordOptions() { RequiredLength = Int32.Parse(PwLength.SelectedValue), IncludeDigit = IncludeNum.Checked, IncludeLowercase = IncludeLowercaseChar.Checked, IncludeUppercase = IncludeUppercaseChar.Checked, IncludeSymbols = IncludeSymbols.Checked }; var passwordList = GenerateRandomPassword(pwOpts); foreach (var pw in passwordList) { passwords += pw + "\n"; } PasswordArea.Value = passwords; //PasswordArea.Value = pwOpts.IncludeDigit.ToString() + pwOpts.IncludeLowercase.ToString() // + pwOpts.IncludeUppercase.ToString() + pwOpts.IncludeSymbols.ToString(); }
public List <string> GenerateRandomPassword(PasswordOptions opts) { var numOfPwNeeded = Int32.Parse(PwQuality.SelectedValue); List <string> passwordList = new List <string>(); List <string> randomChars = new List <string>(); Random rand = new Random(Environment.TickCount); for (int j = 0; j < numOfPwNeeded; j++) { List <char> chars = new List <char>(); if (opts == null) { opts = new PasswordOptions() { RequiredLength = 8, IncludeDigit = true, IncludeLowercase = true, IncludeUppercase = true, IncludeSymbols = true, }; PasswordArea.Value = opts.RequiredLength.ToString() + opts.IncludeDigit.ToString() + opts.IncludeLowercase.ToString() + opts.IncludeUppercase.ToString() + opts.IncludeSymbols.ToString(); } if (opts.IncludeDigit) { randomChars.Add("0123456789"); } if (opts.IncludeLowercase) { randomChars.Add("abcdefghijkmnopqrstuvwxyz"); } if (opts.IncludeUppercase) { randomChars.Add("ABCDEFGHJKLMNOPQRSTUVWXYZ"); } if (opts.IncludeSymbols) { randomChars.Add("~!@#$%^&*()-+=?/<>|[]{}_ :;.,\\`."); } for (int i = chars.Count; i < opts.RequiredLength; i++) { string rcs = randomChars[rand.Next(0, randomChars.Count)]; chars.Insert(rand.Next(0, chars.Count), rcs[rand.Next(0, rcs.Length)]); } passwordList.Add(new string(chars.ToArray())); } return(passwordList); }