How to generate OTP Code in c#?

How to generate OTP Code in c#?, someone asked me to explain?

In this tutorial I will show you how to generate OTP code in c# mvc. Nowadays most applications are using two factor verification to validate the user’s login information using mobile number for online banking or made purchase online goods.

In order to implement the otp verification in c# mvc. Add an empty controller by right click on the controllers folder and name as your wish I named as OTP. Copy and paste the following. 

        public ActionResult GetNumericOTP()
            string numbers = "0123456789";
            Random random = new Random();
            string otp = string.Empty;
            for (int i = 0; i < 5; i++)
                int tempval = random.Next(0, numbers.Length);
                otp += tempval;
            return Json(otp, JsonRequestBehavior.AllowGet);


Right click on the index and add a view. Copy and paste the following. Here I have implemented using jQuery ajax method make a call to a controller function GetNumericOTP function return as json string result. 

    ViewBag.Title = "Generate Numeric OTP";
<script src=""></script>
@using (Html.BeginForm())
            <td>Click on button to Generate Numeric OTP :</td>
            <td><input id="btnGenerateOTP" type="submit" value="Generate OTP" /></td>
            <td><p style="color:mediumvioletred" id="result"></p></td>
<script type="text/javascript">
    $(function () {
        $("#btnGenerateOTP").click(function () {
                type: 'POST',
                url: '/OTP/GetNumericOTP',
                data: {},
                contentType: 'application/json; charset=utf-8',
                dataType: 'json',
                success: function (response) {
                error: function (xhr, ajaxOptions, thrownError) {
            return false;

generate random otp in c#

Below video explain about how to implement otp in mvc.

Post your comments / questions