How To Send One Time Password On Registered Mobile Number Using C#

Introduction

In this article, you will learn how to send a One Time Password[OTP] on registered mobile number using C# and asp.net.

Step 1

First create one web page in your Visual Studio and design it. Design is given below,

<asp:Panel ID="pnl1" runat="server">
  <table>
    <tr>
      <td>Enter Your Mobile Number:</td>
      <td>
        <asp:TextBox ID="txtmobileNo" runat="server"></asp:TextBox>
      </td>
    </tr>
    <tr>
      <td></td>
      <td>
        <asp:Button ID="btnsendOtp" runat="server" Text="Send OTP" OnClick="btnsendOtp_Click" />
      </td>
    </tr>
  </table>
</asp:Panel>
<asp:Panel ID="pnl2" runat="server" Visible="false">
  <table>
    <tr>
      <td>Enter Your OTP:</td>
      <td>
        <asp:TextBox ID="txtverifyMobileNO" runat="server"></asp:TextBox>
      </td>
    </tr>
    <tr>
      <td></td>
      <td>
        <asp:Button ID="btnverify" runat="server" Text="Verify" OnClick="btnverify_Click" />
      </td>
    </tr>
  </table>
</asp:Panel>

Step 2

Add below namespace in .cs page,

using System.Data.SqlClient;
using System.Data;
using System.Net;
using System.Web.ClientServices;
using System.Collections.Specialized;
using System.Configuration;

Step 3

For sending OTP you need API Key. Register your details on the below link for API KEY and get 10 SMS for free.

https://www.textlocal.in/

Step 4

After registering successfully go to setting option and click on API Key for API key generatiton

Step 5

Click on create API Key and no need to enter IP address and notes just save it.

Step 6

Write the below code on btnOtp_click 

protected void btnsendOtp_Click(object sender, EventArgs e) {
    pnl1.Visible = false;
    pnl2.Visible = true;
    Random random = new Random();
    int value = random.Next(1001, 9999);
    string destinationaddress = "+91" + txtmobileNo.Text;
    string message = "Your OTP is " + value + "(Send by R.R.Research and development founder is Ramesh Chandra)";
    string message1 = HttpUtility.UrlEncode(message);
    using(var wb = new WebClient()) {
        byte[] response = wb.UploadValues("https://api.textlocal.in/send/", new NameValueCollection() {
            {
                "apikey",
                "here is enter your API Key"
            }, {
                "numbers",
                destinationaddress
            }, {
                "message",
                message1
            }, {
                "sender",
                "TXTLCL"
            }
        });
        string result = System.Text.Encoding.UTF8.GetString(response);
        Session["OTP"] = value;
    }
}

Step 6

Verify your OTP. Write the below code on verify button.

protected void btnverify_Click(object sender, EventArgs e) {
    if (txtverifyMobileNO.Text == Session["OTP"].ToString()) {
        pnl2.Visible = false;
        ScriptManager.RegisterStartupScript(this, typeof(string), "Message", "confirm('Your Mobilenumber has been verify sccessfully.');", true);
    } else {
        ScriptManager.RegisterStartupScript(this, typeof(string), "Message", "confirm('Your OTP is not correct please enter correct OTP');", true);
        pnl2.Visible = true;
    }
}

Step 7

Now run the project.

Output

Step 8

After entering the mobile number and click on send OTP first panel will be false and the second panel will be true.

Step 9

Enter OTP and click on verify button, you will get the below message.

"Your Mobile number has been verified sccessfully."