In this article I will explain with an example, how to generate Unique Random OTP i.e. One Time Password in ASP.Net using C# and VB.Net.
OTPs or One Time Passwords are widely used by banks and other firms to validate the Mobile Numbers of their users. OTPs can be Alphanumeric as well as Numeric and generally have length between 5-10 characters.
The simplest example of an OTP is when you try to connect to a public Wi-Fi network, your mobile device receives an SMS containing a unique password and once you register with the correct password, the device gets connected to the network.
 
 
HTML Markup
The following HTML Markup consists of an ASP. Net DropDownList, a RadioButtonList, a Button and a Label.
The DropDownList contains the Length options and the RadioButtonList contains the Alphanumeric and Numeric options.
<table border="0" cellpadding="0" cellspacing="0">
    <tr>
        <td>
            <asp:DropDownList ID="ddlLength" runat="server">
                <asp:ListItem Text="5" Value="5" />
                <asp:ListItem Text="8" Value="8" />
                <asp:ListItem Text="10" Value="10" />
            </asp:DropDownList>
        </td>
        <td>
            <asp:RadioButtonList ID="rbType" runat="server" RepeatDirection="Horizontal">
                <asp:ListItem Text="Alphanumeric" Value="1" Selected="True" />
                <asp:ListItem Text="Numeric" Value="2" />
            </asp:RadioButtonList>
        </td>
        <td>
            <asp:Button ID="btnGenerate" Text="Generate OTP" runat="server" OnClick="GenerateOTP" />
        </td>
    </tr>
    <tr>
        <td>
            &nbsp;
        </td>
    </tr>
    <tr>
        <td colspan="3">
            OTP:
            <asp:Label ID="lblOTP" runat="server" />
        </td>
    </tr>
</table>
 
 
Generating Unique Random OTP (One Time Password) in ASP.Net
When the Button is clicked, the length and the type values of the Password are fetched from the DropDownList and RadioButtonList.
There are three strings, first containing Upper Case Alphabets, second containing Lower Case Alphabets and the third one containing the 10 digits.
Then a loop is executed and inside the loop a random number is used to fetch the character from the combination of Alphabets and Numeric strings or only the Numeric string based on the type selected in the RadioButtonList.
Inside the For Loop, a While loop is used to avoid repetition of the characters.
The generated unique random OTP i.e. One Time Password is displayed in the Label control.
C#
protected void GenerateOTP(object sender, EventArgs e)
{
    string alphabets = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    string small_alphabets = "abcdefghijklmnopqrstuvwxyz";
    string numbers = "1234567890";
 
    string characters = numbers;
    if (rbType.SelectedItem.Value == "1")
    {
        characters += alphabets + small_alphabets + numbers;
    }
    int length = int.Parse(ddlLength.SelectedItem.Value);
    string otp = string.Empty;
    for (int i = 0; i < length; i++)
    {
        string character = string.Empty;
        do
        {
            int index = new Random().Next(0, characters.Length);
            character = characters.ToCharArray()[index].ToString();
        } while (otp.IndexOf(character) != -1);
        otp += character;
    }
    lblOTP.Text = otp;
}
 
VB.Net
Protected Sub GenerateOTP(sender As Object, e As EventArgs)
    Dim alphabets As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    Dim small_alphabets As String = "abcdefghijklmnopqrstuvwxyz"
    Dim numbers As String = "1234567890"
 
    Dim characters As String = numbers
    If rbType.SelectedItem.Value = "1" Then
        characters += Convert.ToString(alphabets & small_alphabets) & numbers
    End If
    Dim length As Integer = Integer.Parse(ddlLength.SelectedItem.Value)
    Dim otp As String = String.Empty
    For i As Integer = 0 To length - 1
        Dim character As String = String.Empty
        Do
            Dim index As Integer = New Random().Next(0, characters.Length)
            character = characters.ToCharArray()(index).ToString()
        Loop While otp.IndexOf(character) <> -1
        otp += character
    Next
    lblOTP.Text = otp
End Sub
 
 
Screenshot
Generate Unique Random OTP (One Time Password) in ASP.Net using C# and VB.Net
 
 
Demo
 
 
Downloads