Generate random password in ASP.Net (C# & VB.Net)

Mohammed Imtiyaz Feb 13, 2015

In this tutorial you will learn to generate the random passwords in ASP.Net

Demo

Generate random password in ASP.Net

ASP.Net Controls

We'll take here two server controls one is TextBox and another one is Button. In the TextBox user may enter the desired length of the password for example if the user want to generate password of 8 characters then he should enter 8 in the Textbox and click the Button.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="VC.aspx.cs" Inherits="_VC" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<title>Generate Random Password in ASP.Net</title>
</head>
<body>
<form id="form1" runat="server">
	<label for="txtPasswordLength">Enter required password length:</label>
    <asp:TextBox ID="txtPasswordLength" runat="server" Columns="2" MaxLength="2" Width="43px"></asp:TextBox>

	<asp:Button ID="btnGeneratePassword" runat="server" Text="Generate Password" OnClick="btnGeneratePassword_Click" />
    <asp:Label ID="lblGeneratedPassword" runat="server" ForeColor="Green" Text=""></asp:Label>
</form>
</body>
</html>

C#

Button click event code

public partial class _VC: System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            lblGeneratedPassword.Text = "Please enter a password length (e.g. 8)";
    }
  
    public static string CreateRandomPassword(int intPasswordLength)
    {
        string strPasswordCharacters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789";
        Random randomNum = new Random();
        char[] chars = new char[intPasswordLength];
        for (int i = 0; i < intPasswordLength; i++)
        {
            chars[i] = strPasswordCharacters[(int)((strPasswordCharacters.Length) * randomNum.NextDouble())];
        }
        return new string(chars);
    }
  
    protected void btnGeneratePassword_Click(object sender, EventArgs e)
    {
        if (txtPasswordLength.Text != "") {
            string strPasswordLength = txtPasswordLength.Text.Trim();
            lblGeneratedPassword.Text = "Your generated password is: " + CreateRandomPassword(int.Parse(strPasswordLength));
        }
    }
}

VB.Net

Button click event code

Partial Class _VB
    Inherits System.Web.UI.Page 

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            lblGeneratedPassword.Text = "Please enter a password length (e.g. 8)"
        End If
    End Sub 

    Public Shared Function CreateRandomPassword(intPasswordLength As Integer) As String
        Dim strPasswordCharacters As String = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789"
        Dim randomNum As New Random()
        Dim chars As Char() = New Char(intPasswordLength - 1) {}
        For i As Integer = 0 To intPasswordLength - 1
            chars(i) = strPasswordCharacters(CInt((strPasswordCharacters.Length) * randomNum.NextDouble()))
        Next
        Return New String(chars)
    End Function 

    Protected Sub btnGeneratePassword_Click(sender As Object, e As System.EventArgs) Handles btnGeneratePassword.Click
        If txtPasswordLength.Text <> "" Then
            Dim strPasswordLength As String = txtPasswordLength.Text.Trim()
            lblGeneratedPassword.Text = "Your generated password is: " + CreateRandomPassword(Integer.Parse(strPasswordLength))
        End If
    End Sub
End Class