Friday, 17 May 2013

Generate Random Password

Description :

In the previous article I discussed how to generate random password using Cryptography concept. But here I will show you how to generate random password using character array

Now Design your aspx page like this.

<%@ Page Language="C#"AutoEventWireup="true"CodeFile="Default.aspx.cs"Inherits="_Default"%>
<!DOCTYPE html PUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""">
<html xmlns="">
<head runat="server">
    <form id="form1" runat="server">
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click"
            Text="Click Here To Generate Random Password" />
        <br />
        <br />
        Your Password is&nbsp;&nbsp;&nbsp;
        <asp:Label ID="Label1" runat="server"></asp:Label>
After that write the following code in code behind.

    protected void Button1_Click(object sender, EventArgse)
        Label1.Text =GenerateRandomPassword(7);

    private string GenerateRandomPassword(int length)
        stringchars = "abcdefghijklmnopqrstuvwxyz1234567890!@#$%^&*ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        char[]password = new char[length];
        Randomrnd=new Random();
        for (int i = 0; i < length; i++)
            password[i] = chars[rnd.Next(0,chars.Length)];

        return new string(password);

No comments:

Post a Comment