In this article I will explain with an example, how to generate unique random OTP (One Time Password) in ASP.Net Core (.Net Core) MVC.
OTPs or One Time Passwords are widely used by banks and other firms to validate the Mobile Numbers of their users. OTPs can be Alphanumeric as well as Numeric and generally have length between 5-10 characters.
The simplest example of an OTP is when you try to connect to a public Wi-Fi network, your mobile device receives an SMS containing a unique password and once you register with the correct password, the device gets connected to the network.
Note: For beginners in ASP.Net Core (.Net Core 7), please refer my article ASP.Net Core 7: Hello World Tutorial with Sample Program example.
 
 

Controller

The controller consists of following Action methods.

Action method for handling GET operation

Inside this Action method, simply the View is returned.
 

Action method for handling POST operation

This Action method gets called when Submit Button is clicked and it accepts selected value of DropDownList and RadioButtonList as parameters.
Note: The DropDownList value will be length of OTP and RadioButtonList value will be type of OTP.
 
The following three string variables are created:
capital_alphabets – For storing Upper Case Alphabets.
small_alphabets – For storing Lower Case Alphabets.
numbers – For storing 10 digits (0-9).
A variable is created with name characters which stores the value of numbers variable.
Next, a FOR loop is executed for the selected length inside which a DO WHILE loop is used to avoid repetition of the characters.
Inside the DO WHILE loop, a random number is used to fetch the character from characters variable based on the type selected in the RadioButtonList.
Finally, the generated unique random OTP i.e. One Time Password is into a ViewBag object.
public class HomeController : Controller
{
    public IActionResult Index()
    {
        return View();
    }
 
    [HttpPost]
    public IActionResult Index(int length, string type)
    {
        string alphabets = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        string small_alphabets "abcdefghijklmnopqrstuvwxyz";
        string numbers = "1234567890";
 
        string characters = numbers;
        if (type == "1")
        {
             characters += alphabets + small_alphabets + numbers;
        }
 
        string otp = string.Empty;
        for (int i = 0; i < length; i++)
        {
            string character = string.Empty;
            do
            {
                int index = new Random().Next(0,characters.Length);
                character = characters.ToCharArray()[index].ToString();
            } while(otp.IndexOf(character) != -1);
            otp += character;
        }
 
        ViewBag.Message otp;
        return View();
    }
}
 
 

View

HTML Markup

The View consists of an HTML Form which has been created using the following TagHelpers attributes.
asp-action – Name of the Action. In this case the name is Index.
asp-controller – Name of the Controller. In this case the name is Home.
method – It specifies the Form Method i.e. GET or POST. In this case it will be set to POST.
The Form consist of an HTML Table which consists of an HTML SELECT (DropDownList) and two RadioButton elements and a Submit button.
 

Submitting the Form

When the Submit Button is clicked, the value of the ViewBag object i.e. Generated OTP is displayed.
@addTagHelper*,Microsoft.AspNetCore.Mvc.TagHelpers
@{
     Layout = null;
}
 
<!DOCTYPE html>
 
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
    <style type="text/css">
        select { width:50px; }
    </style>
</head>
<body>
    <form method="post" asp-controller="Home" asp-action="Index">
        <table border="0" cellpadding="0" cellspacing="0">
            <tr>
                <td>
                    <select name="length">
                        <option value="">Please select</option>
                        <option value="5">5</option>
                        <option value="8">8</option>
                        <option value="10">10</option>
                    </select>
                </td>
                <td>
                    <input name="type" type="radio" value="1" />Alphanumeric
                    <input name="type" type="radio" value="2" />Numeric
                </td>
                td><input type="submit" value="Genderate OTP" /></td
            </tr>
            <tr>
                <td>&nbsp;</td
            </tr>
            <tr>
                <td colspan="3">
                     OTP:@ViewBag.Message
                </td>
            </tr>
        </table>
    </form>
</body>
</html>
 
 

Screenshot

ASP.Net Core: Generate Unique Random OTP (One Time Password)
 
 

Demos

 
 

Downloads