En el lado del cliente agregamos este codigo(Default.aspx)
<form id=”form1″ runat=”server”> <div> <asp:TextBox ID=”TextBox1″ runat=”server”></asp:TextBox> <asp:Button ID=”rndpwd” runat=”server” Text=”Generate Pwd” onclick=”rndpwd_Click” /> <br /> <asp:Label ID=”Label1″ runat=”server” Text=”Label”></asp:Label> </div> </form>Ahora creamos una clase en el app_code y escribimos este metodo ahi:
public static string GetRandomPassword(int length)
{
char[] chars = "$%#@!*abcdefghijklmnopqrstuvwxyz1234567890?;:ABCDEFGHIJKLMNOPQRSTUVWXYZ^&".ToCharArray();
string password = string.Empty;
Random random = new Random();
for (int i = 0; i < length; i++)
{
int x = random.Next(1,chars.Length);
//Don't Allow Repetation of Characters
if (!password.Contains(chars.GetValue(x).ToString()))
password += chars.GetValue(x);
else
i--;
}
return password;
}
Llamamos esta pagina en el lado del servidor utilizando el evento click del boton de la siguiente manera:
protected void rndpwd_Click(object sender, EventArgs e)
{
Label1.Text = PasswordGeneration.GetRandomPassword(Convert.ToInt32(TextBox1.Text));
}y listo ejecuten el codigo ingresan un numero y les saldra un password de esa longitud


0 comentarios:
Publicar un comentario