I need to create a random Password but that it complies with some specific parameters:
Must have a mayus
Must have numbers
It must have special characters.
It can not contain the following strings "123", "12345", "56789", "123456789", "321", "54321", "987654321", "qwerty", "asdf", "zxcv", "poiuy", "lkjhg", "mnbv"
Among other.
I already did it with the following code, but it throws me an error of StackOberflowException, of what another way I can achieve it or what would be the solution to this error?
public static string CrearPassword(int longitud,string usuario)
{
string caracteres = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNÑOPQRSTUVWXYZ1234567890ñÑ-_¿.#¡";
StringBuilder res = new StringBuilder();
Random rnd = new Random();
while (0 < longitud--)
{
res.Append(caracteres[rnd.Next(caracteres.Length)]);
}
while (ValidPassword(res.ToString(), usuario)== false)
{
return CrearPassword(13,usuario);
}
return res.ToString();
}
public static bool ValidPassword(string pass, string usuario)
{
try
{
Match matchLongitud = Regex.Match(pass, @"^\w{8,15}\b");
Match matchNumeros = Regex.Match(pass, @"\d");
Match matchEspeciales = Regex.Match(pass, @"[ñÑ\-_¿.#¡]");
Match matchMayusculas = Regex.Match(pass, @"[A-Z]");
Match matchAdmin = Regex.Match(pass, @"admin");
Match matchContraseña = Regex.Match(pass, @"contraseña");
Match matchNombreUsuario = Regex.Match(pass, usuario);
var valoresProhibidos = new List<string>() { "123", "12345", "56789", "123456789", "321", "54321", "987654321", "qwerty", "asdf", "zxcv", "poiuy", "lkjhg", "mnbv" };
if (!matchNumeros.Success)
return false;
else if (!matchLongitud.Success)
return false;
else if (!matchEspeciales.Success)
return false;
else if (!matchMayusculas.Success)
return false;
else if (matchAdmin.Success)
return false;
else if (matchContraseña.Success)
return false;
else if (matchNombreUsuario.Success)
return false;
else
{
foreach (string valor in valoresProhibidos)
{
if (pass.Contains(valor))
{
return false;
}
}
}
return true;
should validate and return the password, but pulls error from SystemStackOverflowException enter image description here