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.
[HttpPost]
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="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
@using (Html.BeginForm())
{
<table>
<tr>
<td>Click on button to Generate Numeric OTP :</td>
<td><input id="btnGenerateOTP" type="submit" value="Generate OTP" /></td>
</tr>
<tr>
<td></td>
<td><p style="color:mediumvioletred" id="result"></p></td>
</tr>
</table>
}
<script type="text/javascript">
$(function () {
$("#btnGenerateOTP").click(function () {
$.ajax({
type: 'POST',
url: '/OTP/GetNumericOTP',
data: {},
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function (response) {
$("#result").html(response);
},
error: function (xhr, ajaxOptions, thrownError) {
}
});
return false;
})
});
</script>
Below video explain about how to implement otp in mvc.
Post your comments / questions
Recent Article
- How to fix HAXM is not installed |in Android Studio
- How to fix CMOS Checksum Error in Computer or Laptop | SOLVED
- Reactivating windows after a Hardware change on PC or Laptop
- FIXED: Windows reported that the hardware of your device has changed. Error code :0xc004F211
- "redirect" is not defined pylance("reportUndefinedVariable)
- This action cannot be completed because the file is open in SQL Server(SQLEXPRESS) - FIXED
- Unicode error 'unicodeescape' codec can't decode bytes in position 2-3: truncated UXXXXXXXX escape
- Could not find the 'angular-devkit/build-angular:dev-server' builder's node package | Angular Error
Related Article