Wednesday, 22 May 2013

Generate Random Password2


Generate Random Password2


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""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <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>
    </div>
    </form>
</body>
</html>
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);
    }
OUTPUT
demo
DOWNLOAD SAMPLE CODE
download 

No comments:

Post a Comment