Reader Level:
ARTICLE

Generate Random Password in ASP.NET

Posted by Rohatash Kumar Articles | ASP.NET Programming March 02, 2012
Here, we will see how to generate random passwords in ASP.NET. Random numbers may be generated in the .NET Framework using the Random class.
  • 0
  • 0
  • 23374
Download Files:
 

Here, we will see how to generate random passwords in ASP.NET. Random numbers may be generated in the .NET Framework using the Random class. You create an instance to an object of Random class and generate random numbers. You have to create a web site and add a new page to the website. Drag and drop two TextBox and one Button control on the form. One TextBox to give the length of the TextBox, another to display the random password of the specified length when we click on the Button control. Let's take a look at a practical example.

First you have to create a web site.

  • Go to Visual Studio 2010
  • New-> Select a website application
  • Click OK

img5.gif

Now add a new page to the website.

  • Go to the Solution Explorer
  • Right Click on the Project name
  • Select add new item
  • Add new web page and give it a name
  • Click OK

img6.gif

Now create a new website and drag and drop two TextBox controls onto the aspx page. The TextBox code looks like this:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="randomnumber.aspx.cs" Inherits="randomnumber" %>

<!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>

       Password Length: &nbsp;&nbsp; <asp:TextBox ID="txtPassLength" runat="server"></asp:TextBox>

        <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>

</html>

In ASP.Net you can use a Random class to generate the random numbers. Create an instance to an object of the Random class and generate random numbers. This class may be instantiated using the following code:

 Random rand = new Random();

In the above rand is an object of the Random Class.

Now double-click on the Button control and add the following code:

C# code

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

 

public partial class randomnumber : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

 

    }

    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 and test it.

img1.gif

Now give the length of the password.

img2.gif

Now click on the Button to generate a random password.

img3.gif

The above output displays a random password.  

Some Helpful Resources

COMMENT USING

Trending up