//Generate a random code for a not logged in user public static string GenerateRandomPassword(Microsoft.AspNetCore.Identity.PasswordOptions opts = null) { if (opts == null) { opts = new Microsoft.AspNetCore.Identity.PasswordOptions() { RequiredLength = 8, RequiredUniqueChars = 4, RequireDigit = true, RequireLowercase = true, RequireNonAlphanumeric = true, RequireUppercase = true } } ; string[] randomChars = new[] { "ABCDEFGHJKLMNOPQRSTUVWXYZ", // uppercase "abcdefghijkmnopqrstuvwxyz", // lowercase "0123456789", // digits "!@$?_-" // non-alphanumeric }; Random rand = new Random(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())); }
public static bool IsValidPassword(string password, Microsoft.AspNetCore.Identity.PasswordOptions options) { return(UtilityManager.IsValidPassword( password, options.RequiredLength, options.RequiredUniqueChars, options.RequireNonAlphanumeric, options.RequireLowercase, options.RequireUppercase, options.RequireDigit)); }