Thursday, 21 February 2013

Random Password Generator ASP.NET C#


Random Password Generator ASP.NET C#


Good Morning/Good Afternoon /Good Evening Guys.. Today I will show you a method which will help you to generate random password dynamically.. It will be helpful to use this code when you want to give your users a Random Password Upon Registration or during Forget Your Password Action..

So Here is the Code Snippet for that:

Design Part:

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Generate Random Password</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Generate Password" /><br />
        <br />
        <asp:Label ID="Label1" runat="server" Font-Bold="True" ForeColor="Red"></asp:Label>&nbsp;</div>
    </form>
</body>
</html>

Code-Behind:


    protected void Button1_Click(object sender, EventArgs e)
    {
        Label1.Text="Your New Password is:"+GeneratePassword(8);
    }

    public string GeneratePassword(int PwdLength)
    {
   string _allowedChars = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ0123456789";
   Random rndNum = new Random();
   char[] chars = new char[PwdLength];
   int strLength = _allowedChars.Length;
   for (int i = 0; i <= PwdLength - 1; i++) {
  chars[i] = _allowedChars[Convert.ToInt32(Math.Floor((_allowedChars.Length) * rndNum.NextDouble()))];
   }
   return new string(chars);
    }

1 comment:

  1. this artical is like life saving hands for fresher like me

    ReplyDelete

Thank You for Your Comments. We will get back to you soon.

back to top