|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- namespace Diligent.WebAPI.Business.Helper
- {
- public static class StringGenerator
- {
- public static string GenerateRandomPassword(PasswordOptions opts = null)
- {
- if (opts == null) opts = new PasswordOptions()
- {
- RequiredLength = 8,
- RequiredUniqueChars = 4,
- RequireDigit = true,
- RequireLowercase = true,
- RequireNonAlphanumeric = true,
- RequireUppercase = true
- };
-
- if(opts.RequiredLength < 4)
- opts.RequiredLength = 4;
-
- string[] randomChars = new[] {
- "ABCDEFGHJKLMNOPQRSTUVWXYZ", // uppercase
- "abcdefghijkmnopqrstuvwxyz", // lowercase
- "0123456789", // digits
- "!@$?_-" // non-alphanumeric
- };
-
- Random rand = new(Environment.TickCount);
- List<char> chars = new List<char>();
-
- if (opts.RequireUppercase)
- chars.Insert(rand.Next(0, chars.Count),
- randomChars[0][rand.Next(0, randomChars[0].Length)]);
-
- if (opts.RequireLowercase)
- chars.Insert(rand.Next(0, chars.Count),
- randomChars[1][rand.Next(0, randomChars[1].Length)]);
-
- if (opts.RequireDigit)
- chars.Insert(rand.Next(0, chars.Count),
- randomChars[2][rand.Next(0, randomChars[2].Length)]);
-
- if (opts.RequireNonAlphanumeric)
- chars.Insert(rand.Next(0, chars.Count),
- randomChars[3][rand.Next(0, randomChars[3].Length)]);
-
- for (int i = chars.Count; i < opts.RequiredLength
- || chars.Distinct().Count() < opts.RequiredUniqueChars; i++)
- {
- string rcs = randomChars[rand.Next(0, randomChars.Length)];
- chars.Insert(rand.Next(0, chars.Count),
- rcs[rand.Next(0, rcs.Length)]);
- }
-
- return new string(chars.ToArray());
- }
- }
- }
|