ARTICLE

How to Generate Random password in ASP.NET using VB.NET

Posted by Rohatash Kumar Articles | ASP.NET using VB.NET August 02, 2011
Here, we will see how to generate random password in ASP.NET.
Download Files:
 
Reader Level:

Here, we will see how to generate random password in ASP.NET. Drag and drop two TextBox and one Control on the form. One TextBox to give the length of the TextBox other to displays random password according to length when we click on the Button control.

The form looks like below figure.

random1.gif

Figure1

.ASPX code

<body>

    <form id="form1" runat="server">

    <div>

        Password Length:

        <asp:TextBox ID="txtPassLength" runat="server"></asp:TextBox>

        <br />

        <br />

        Random Password:

        <asp:TextBox ID="txtpassword" runat="server"></asp:TextBox>

        <br />

        <br />

        <asp:Button ID="Button1" runat="server" onclick="Button1_Click"

            Text="Generate Password" />

        <br />

   

    </div>

    </form>

</body>

Now double click on the Button control and add the following code.

VB code

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click

        Dim allowedChars As String = ""

        allowedChars = "a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,"

        allowedChars += "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,"

        allowedChars += "1,2,3,4,5,6,7,8,9,0,!,@,#,$,%,&,?"

        Dim sep As Char() = {","c}

        Dim arr As String() = allowedChars.Split(sep)

        Dim passwordString As String = ""

        Dim temp As String = ""

        Dim rand As New Random()

        For i As Integer = 0 To Convert.ToInt32(txtPassLength.Text) - 1

            temp = arr(rand.[Next](0, arr.Length))

            passwordString += temp

        Next

        txtpassword.Text = passwordString

    End Sub

C# code

protected void Button1_Click(object sender, EventArgs e)

        {

            string allowedChars = "";

            allowedChars = "a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,";

            allowedChars += "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,";

            allowedChars += "1,2,3,4,5,6,7,8,9,0,!,@,#,$,%,&,?";

            char[] sep = { ',' };

            string[] arr = allowedChars.Split(sep);

            string passwordString = "";

            string temp = "";

            Random rand = new Random();

            for (int i = 0; i < Convert.ToInt32(txtPassLength.Text); i++)

            {

                temp = arr[rand.Next(0, arr.Length)];

                passwordString += temp;

            }

            txtpassword.Text = passwordString;

        }

 

Now run the application.

random2.gif

Figure2

Now give the length of the password.

random3.gif

Figure3

Now click on the Button to generate random password.

random4.gif

Figure4

Login to add your contents and source code to this article
share this article :
post comment
 
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Become a Sponsor