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 HTML Markup consists of following controls:
DropDownList – For capturing length of OTP.
RadioButtonList – For capturing type of the OTP i.e. Alphanumeric and Numeric.
Button – For generating OTP.
The Button has been assigned with an OnClick event handler.
Label – For displaying generated OTP.
<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 Generate OTP Button is clicked, the following three string variables are created.
capital_alphabets – For storing Upper Case Alphabets.
small_alphabets – For storing Lower Case Alphabets.
numbers – For storing 10 digits (0-9).
A variable is created with name characters which stores the value of numbers variable.
After that, it checks if the selected type of OTP is 1 which indicates that OTP must be Alphanumeric in nature. Inside this condition, the string variables i.e. capital_alphabets, small_alphabets and numbers are concatenated and stored into a characters variable.
Next, a FOR loop is executed for the selected length inside which a DO WHILE loop is used to avoid repetition of the characters.
Note: The DropDownList value will be the length of OTP and RadioButtonList value will be the type of OTP i.e. Alphanumeric and Numeric.
 
Inside the DO WHILE loop, a random number is used to fetch the character from characters variable based on the type selected in the RadioButtonList.
Finally, the generated unique random OTP i.e. One Time Password is set into 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