Generate unique OTP in asp.net using C#, vb.net - Free Asp.Net,MVC,AngularJs,Jquery,Javascript,Sql Server,WCF,Entity framework snippets and tutorial

Generate unique OTP in asp.net using C#, vb.net

In this article I am going to explain how to generate unique OTP (one time password) in asp.net

Description:

OTP stand for one time password. It is a password that is valid for only one login session or transaction. It is widely used by the banks for transactions. It can be numeric as well as alphanumerical.

Implementation:

Method (code) to generate the numeric OTP:
Add a webform to project. Drag and drop the required control from toolbox to webform. Below given html markup consist of button and label control.

HTML markup:
   <fieldset style="width:420px">
    <legend><strong>OTP Example</strong></legend>
     <table>
    <tr><td>Click to Generate OTP :</td><td>
        <asp:Button ID="Button1" runat="server" Text="Generate OTP"/></td></tr>
     <tr><td></td><td></td></tr>
      <tr id="idotp" runat="server" visible="false"><td><strong>OTP</strong> :</td><td>
          <asp:Label ID="lblotp" runat="server"></asp:Label></td></tr>
    </table>
    </fieldset>

Generate numeric OTP
On button click write the below given to generate OTP:
C# code:

protected void Button1_Click(object sender, EventArgs e)
    {
        GenerateOTP();
    }

    private void GenerateOTP()
    {
        try
        {
            int length = 6;
            string numbers = "0123456789";
            Random objrandom = new Random();
            string strrandom = string.Empty;
            int noofnumbers = length;
            for (int i = 0; i < noofnumbers; i++)
            {
                int temp = objrandom.Next(0, numbers.Length);
                strrandom += temp;
            }
            lblotp.Text = strrandom;
            idotp.Visible = true;
        }
        catch (Exception ex)
        {
        }
    }

VB.net Code:
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        GenerateOTP()
    End Sub

    Private Sub GenerateOTP()
        Try
            Dim length As Integer = 6
            Dim numbers As String = "0123456789"
            Dim objrandom As New Random()
            Dim strrandom As String = String.Empty
            Dim noofnumbers As Integer = length
            For i As Integer = 0 To noofnumbers - 1
                Dim temp As Integer = objrandom.[Next](0, numbers.Length)
                strrandom += temp
            Next
            lblotp.Text = strrandom
            idotp.Visible = True
        Catch ex As Exception
        End Try
    End Sub


Method (code) to generate Alphanumeric OTP:

HTML Markup:
   <fieldset style="width:420px">
    <legend><strong>OTP Example</strong></legend>
     <table>
    <tr><td>Click to Generate OTP :</td><td>
        <asp:Button ID="Button1" runat="server" Text="Generate OTP"/></td></tr>
     <tr><td></td><td></td></tr>
      <tr id="idotp" runat="server" visible="false"><td><strong>OTP</strong> :</td><td>
          <asp:Label ID="lblotp" runat="server"></asp:Label></td></tr>
    </table>
    </fieldset>

Generate alphanumeric OTP:
Write the below given code on button click event to generate the alphanumeric OTP.
C# Code:
  protected void Button1_Click(object sender, EventArgs e)
    {
        GenerateOTP(10);
    }
 
  private void GenerateOTP(int length)
    {
        try
        {
            string numbers = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";           
            Random objrandom = new Random();
            string strrandom = string.Empty;
            string passwordString = "";
            string opt = "";
            int noofnumbers = length;
            for (int i = 0; i < noofnumbers; i++)
            {
                int temp = objrandom.Next(0, numbers.Length);
                passwordString = numbers.ToCharArray()[temp].ToString();
                strrandom += passwordString;
            }
            while (opt.IndexOf(passwordString) != -1) ;
            lblotp.Text = strrandom;
            idotp.Visible = true;
        }
        catch (Exception ex)
        {
        }
    }

VB.net Code: 
    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        GenerateOTP(10)
    End Sub
  
 Private Sub GenerateOTP(ByVal length As Integer)
        Try
            Dim numbers As String = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
            Dim objrandom As New Random()
            Dim strrandom As String = String.Empty
            Dim passwordString As String = ""
            Dim opt As String = ""
            Dim noofnumbers As Integer = length
            For i As Integer = 0 To noofnumbers - 1
                Dim temp As Integer = objrandom.[Next](0, numbers.Length)
                passwordString = numbers.ToCharArray()(temp).ToString()
                strrandom += passwordString
            Next
            While opt.IndexOf(passwordString) <> -1
            End While
            lblotp.Text = strrandom
            idotp.Visible = True
        Catch ex As Exception
        End Try
    End Sub 


Build the application and run. To test the working click on generate OTP button.

DEMO:
Generate unique OTP in asp.net using C#, vb.net
 In this article we have learn how to generate unique OTP in asp.net using C# and vb.net . I hope you enjoyed this article.

What do you think about this article?

If you found this article useful, please share and follow on Facebook, Twitter, Google Plus and other social media websites. To get free updates subscribe to newsletter. Please put your thoughts and feedback in comments section.

Share this

Share on FacebookTweet on TwitterPlus on Google+

2 comments


EmoticonEmoticon