Monday, June 20, 2016

Generate Random Password

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

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

Now create a new website and drag and drop two TextBox controls onto the aspx page. The TextBoxcode 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.

Now give the length of the password.

Now click on the Button to generate a random password.


The above output displays a random password.