How to Send and Verify OTP On Mobile No Using C# In Asp.Net

How to Send and Verify OTP On Mobile No Using C# In Asp.Net

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SendOTP.aspx.cs" Inherits="Shoping_Cart.SendOTP" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>How to Send and Verify OTP On Mobile No Using C# In Asp.Net</title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            Mobile No:
        <asp:TextBox ID="txtMobileNo" runat="server" Width="200px"></asp:TextBox>
             <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Send OTP" />

            <br />
            <asp:Label ID="lblMessage" runat="server" Text=""></asp:Label>
        </div>
    </form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Shoping_Cart
{
    public partial class SendOTP : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            try
            {
                Random r = new Random();
                string OTP = r.Next(1000, 9999).ToString();

                //Send message
                string Username = "youremail@domain.com";
                string APIKey = "YourHash";
                string SenderName = "MyName";
                string Number = "981111111";
                string Message = "OTP code is - " + OTP;
                string URL = "http://api.minifycode.in/send/?username=" + Username + "&hash=" + APIKey + "&sender=" + SenderName + "&numbers=" + Number + "&message=" + Message;
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(URL);
                HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
                StreamReader sr = new StreamReader(resp.GetResponseStream());
                string results = sr.ReadToEnd();
                sr.Close();
                
                Session["OTP"] = OTP;

                //Redirect for varification
                Response.Redirect("VerifyOTP.aspx");
            }
            catch (Exception ex)
            {
                lblMessage.Text = ex.Message.ToString();
            }
        }

    }
}
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Verify.aspx.cs" Inherits="Cart.VerifyOTP" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Verify OTP</title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            OTP:
        <asp:TextBox ID="txtOTP" runat="server" Width="200px"></asp:TextBox>
             
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Verify OTP" />
            <br />
            <asp:Label ID="lblMessage" runat="server" Text=""></asp:Label>
        </div>
    </form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Cart
{
    public partial class OTP : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (Session["OTP"].ToString() == txtOTP.Text)
            {
                lblMessage.Text = "You have enter correct OTP.";
                Session["OTP"] = null;
            }
            else
            {
                lblMessage.Text = "Pleae enter correct OTP.";
            }
        }
    }
}

2020-03-16