How to Generate One Time Password (OTP) in Asp.Net using C#, Vb.Net

In this tutorial i am going to share with you how to generate or create one time password (OTP) in Asp.Net using Both C# and Vb.Net.


One time password (OTP) is a random unique alphanumeric number. This system is widely used by banks and other firms to authenticate users while making online transactions and other similar activities. In this tutorial i have created a web application to generate random unique alphanumeric number called one time password (OTP) which is combination of special symbols, Uppercase or Lowercase latter. Generated OTP you can be sent through via SMS  or Email to user for authentication purpose.

So, Let's create one web application for demonstration purpose.

HTML CODE:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>OTP</title>
    <style type="text/css">
        .auto-style1 {
            height54px;
        }
        .auto-style2 {
            width151px;
        }
        .auto-style3 {
            color#FF9900;
        }
        .auto-style4 {
            color#FF9900;
            height29px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <table>
                <tr>
                    <td class="auto-style1" colspan="2" style="text-aligncenter">
                        <h1 class="auto-style3"><strong>One Time Password(OTP)</strong></h1>
                    </td>
                </tr>
                <tr>
                    <td>Enter Number of Characters:</td>
                    <td class="auto-style2">
            <asp:TextBox ID="txtOTPpass" runat="server" Width="146px" />
                    </td>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                    <td class="auto-style2">&nbsp;</td>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                    <td class="auto-style2"><asp:Button ID="btnGenerateOTPpass" Text="Generate" runat="server"
                        OnClick="btnGenerateOTPpass_Click" BackColor="#009900" BorderStyle="None" ForeColor="White" Height="33px" Width="81px" /></td>
                </tr>
                <tr>
                    <td style="text-alignright">OTP: </td>
                    <td class="auto-style2">
                    <asp:Label ID="lblGetOTP" runat="server" ForeColor="blue" style="color#FF0000font-weight700" />
                    </td>
                </tr>
                <tr>
                    <td style="text-alignright">&nbsp;</td>
                    <td class="auto-style2">&nbsp;</td>
                </tr>
                <tr>
                    <td class="auto-style4" colspan="2" style="text-aligncenterbackground-color#000000;"><strong>Developed By: Nikunj Satasiya</strong></td>
                </tr>
            </table>
            <br />
        </div>
    </form>
</body>
</html>

Asp.Net C# Code to generate One Time Password (OTP)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
public partial class _Default : System.Web.UI.Page
{
    protected void btnGenerateOTPpass_Click(object sender, EventArgs e)
    {
        lblGetOTP.Text = GenerateOTP(Convert.ToInt32(txtOTPpass.Text.Trim()));
    }
    public string GenerateOTP(int Length)
    {
        string _allowedChar = "#@$&*ABCDEFGHJKLMNOPQRSTUVWXYZ0123456789abcdefghijkmnopqrstuvwxyz";
        Random randomNum = new Random();
        char[] chars = new char[Length];
 
        for (int i = 0; i < Length; i++)
        {
            chars[i] = _allowedChar[Convert.ToInt32((_allowedChar.Length - 1) * randomNum.NextDouble())];
        }
        return new string(chars);
    }
}

Asp.Net Vb.Net Code to generate One Time Password (OTP)

Partial Class Default2
    Inherits System.Web.UI.Page
    Public Function GenerateOTP(Length As IntegerAs String
        Dim _allowedChar As String = "#@$&*ABCDEFGHJKLMNOPQRSTUVWXYZ0123456789abcdefghijkmnopqrstuvwxyz"
        Dim randomNum As New Random()
        Dim chars As Char() = New Char(Length - 1) {}
 
        For i As Integer = 0 To Length - 1
            chars(i) = _allowedChar(Convert.ToInt32((_allowedChar.Length - 1) * randomNum.NextDouble()))
        Next
        Return New String(chars)
    End Function
    Protected Sub btnGenerateOTP_Click(sender As Object, e As EventArgsHandles btnGenerateOTPpass.Click
        lblGetOTP.Text = GenerateOTP(Convert.ToInt32(txtOTPpass.Text.Trim()))
    End Sub
End Class
Previous
Next Post »

If you have any kind of question about any post, Feel free to ask.You can simply drop a comment below post. Your feedback and suggestions will be highly appreciated. ConversionConversion EmoticonEmoticon