How to generate OTP(One time password) in asp.net

Description:-

A one-time password (OTP) is a password that is valid for only one login session or transaction, on a computer system or other digital device. ... The most important advantage that is addressed by OTPs is that, in contrast to static passwords, they are not vulnerable to replay attacks.

What is OTP number?

It is a unique 6-character code that can only be used once and is sent only to your registered mobile number in BDO Online Banking. After encoding your user ID and password, you will also be required to enter the correct OTP to complete the login process.

What does it mean to OTP?

The Meaning of OTP. OTP means "One True Pairing" So now you know - OTP means "One True Pairing" - don't thank us. YW! ... OTP is an acronym, abbreviation or slang word that is explained above where the OTP definition is given.

Step 1: Create TextBox,Button,Lable Controls like

<div>
   Enter Number of Text :
   <asp:TextBox ID="txtOTP" runat="server" />
   <asp:Button ID="btnGenerateOTP" Text="Generate" runat="server"       OnClick="btnGenerateOTP_Click" />
   <br />
   <asp:Label ID="lblOTP" runat="server" ForeColor="blue" />
</div>

Step 2: Now using JavaScript Code We Will Generate OTP Through Google-API

<script type="text/javascript">
   function googleTranslateElementInit() {   
       new google.translate.TranslateElement({ pageLanguage: 'en', layout: google.translate.TranslateElement.InlineLayout.SIMPLE }, 'google_translate_element');   
   }   
</script>

<script type="text/javascript">
   (function () {   
       var d = "text/javascript", e = "text/css", f = "stylesheet", g = "script", h = "link", k = "head", l = "complete", m = "UTF-8", n = "."; function p(b) { var a = document.getElementsByTagName(k)[0]; a || (a = document.body.parentNode.appendChild(document.createElement(k))); a.appendChild(b) } function _loadJs(b) { var a = document.createElement(g); a.type = d; a.charset = m; a.src = b; p(a) } function _loadCss(b) { var a = document.createElement(h); a.type = e; a.rel = f; a.charset = m; a.href = b; p(a) } function _isNS(b) { b = b.split(n); for (var a = window, c = 0; c < b.length; ++c) if (!(a = a[b[c]])) return !1; return !0 }
   
       function _setupNS(b) { b = b.split(n); for (var a = window, c = 0; c < b.length; ++c) a.hasOwnProperty ? a.hasOwnProperty(b[c]) ? a = a[b[c]] : a = a[b[c]] = {} : a = a[b[c]] || (a[b[c]] = {}); return a } window.addEventListener && "undefined" == typeof document.readyState && window.addEventListener("DOMContentLoaded", function () { document.readyState = l }, !1);
   
       if (_isNS('google.translate.Element')) { return } (function () { var c = _setupNS('google.translate._const'); c._cl = 'en'; c._cuc = 'googleTranslateElementInit'; c._cac = ''; c._cam = ''; c._ctkk = '403687'; var h = 'translate.googleapis.com'; var s = (true ? 'https' : window.location.protocol == 'https:' ? 'https' : 'http') + '://'; var b = s + h; c._pah = h; c._pas = s; c._pbi = b + '/translate_static/img/te_bk.gif'; c._pci = b + '/translate_static/img/te_ctrl3.gif'; c._pli = b + '/translate_static/img/loading.gif'; c._plla = h + '/translate_a/l'; c._pmi = b + '/translate_static/img/mini_google.png'; c._ps = b + '/translate_static/css/translateelement.css'; c._puh = 'translate.google.com'; _loadCss(c._ps); _loadJs(b + '/translate_static/js/element/main.js'); })();   
   })();   
</script>

Step 3: Generate Button Click Event And Code it

protected void btnGenerateOTP_Click(object sender, EventArgs e)
{
    lblOTP.Text = GenerateOTP(Convert.ToInt32(txtOTP.Text.Trim()));
}

Step 4: Create Method to Generate OTP and Call it in Button Click Event

public string GenerateOTP(int Length)
{
    string _allowedChars = "#@$&*abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ0123456789";
    Random randNum = new Random();
    char[] chars = new char[Length];

    for (int i = 0; i < Length; i++)
    {
        chars[i] = _allowedChars[Convert.ToInt32((_allowedChars.Length - 1) * randNum.NextDouble())];
    }
    return new string(chars);
}

Step 5: Now Run Your Application

 Step 5: Now Insert Number of Text in Textbox to Generate OTP


Related Posts

First

1 comments:

comments
August 26, 2019 at 4:23:00 PM GMT+5:30 delete

OTP SMS delivers instantly for Mobile Number Verification through HTTP API, XML API and Json API with 2 factor authentication.

Reply
avatar

Thanks for comments.....