Friday, March 16, 2018

How to generate numeric and alphanumeric OTP (one time password) in MVC application


Here in this article I am going to explain how to generate numeric and alphanumeric OTP (one time password) in MVC application.

Description:
I want to generate OTP. OTP can be numeric or alphanumeric and it is used widely to validate the user’s information such as mobile number, in banks for transaction etc.

Implementation:

Add controller
Add an empty controller to project. Create 3 action to generate OTP, one to generate numeric and 2nd one alphanumeric.

Complete code of controller:

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using infinitescroll.Models;
using System.Web.Security;

namespace infinitescroll.Controllers
{
    public class OTP : Controller
    {
       
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult GenerateOTP()
        {
            return View();
        }
        [HttpPost]
        public ActionResult GenerateNumericOTP()
        {
            string numbers = "0123456789";
            Random objrandom = new Random();
            string strrandom = string.Empty;
            for (int i = 0; i < 5; i++)
            {
                int temp = objrandom.Next(0, numbers.Length);
                strrandom += temp;
            }
            ViewBag.otp = strrandom;
            return View("GenerateOTP");
        }
        [HttpPost]
        public ActionResult GenerateAlphaNumericOTP()
        {
            string numbers = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
            Random objrandom = new Random();
            string passwordString = "";
            string strrandom = string.Empty;
            for (int i = 0; i < 8; i++)
            {
                int temp = objrandom.Next(0, numbers.Length);
                passwordString = numbers.ToCharArray()[temp].ToString();
                strrandom += passwordString;
            }
            ViewBag.anotp = strrandom;
            return View("GenerateOTP");
        }
    }
}



Add view
Add view for GenerateOTP action.

Complete source of View:


@{
    ViewBag.Title = "Generate OTP";
}

@using (Html.BeginForm("GenerateNumericOTP", "OTP"))
{
    <table>
        <tr>
            <td>Click on button to Generate Numeric OTP :</td>
            <td><input id="Submit1" type="submit" value="Generate OTP" /></td>
        </tr>
        <tr>
            <td></td>
            <td>@ViewBag.otp</td>
        </tr>
    </table>
}
@using (Html.BeginForm("GenerateAlphaNumericOTP", "OTP"))
{
    <table>
        <tr>
            <td>Click on button to Generate Alphanumeric OTP :</td>
            <td><input id="Submit1" type="submit" value="Generate OTP" /></td>
        </tr>
        <tr>
            <td></td>
            <td>@ViewBag.anotp</td>
        </tr>
    </table>
}







No comments:

Post a Comment