2

I am able to send the SMS message to user from our application using twilio.

Here is the link for the sending the message to user through Twilio How to send sms using C# and twilio API

Now I want to generate the OTP(one time password). Send the OTP to user by twilio. User have to reply the OTP to twilio number Is it possible in twilio ?

If Yes, how to reply the OTP SMS message to Twilio number.

Can somebody please help me and show/give some examples?

prasanthi
  • 562
  • 1
  • 9
  • 25

2 Answers2

0

You must generate the OTP and send it by SMS save it and verify it you, Twillo does not generate that OTP.

This code generates a OTP:

protected void GenerateOTP(object sender, EventArgs e)
{
    string alphabets = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    string small_alphabets = "abcdefghijklmnopqrstuvwxyz";
    string numbers = "1234567890";

    string characters = numbers;
    if (rbType.SelectedItem.Value == "1")
    {
         characters += alphabets + small_alphabets + numbers;
    }
    int length = int.Parse(ddlLength.SelectedItem.Value);
    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;
    }
    lblOTP.Text = otp;
 }
  • I will agree with your answer twilio won't generate otp .Then how should i verify the otp which was send by user to twilio registered number – prasanthi Aug 29 '18 at 13:16
  • 2
    I store the code sent, and by an API I receive the code entered by the user and valid. Soon this information will help you a little more https://www.twilio.com/docs/authy/api/one-time-passwords – Miguel Andres Arias Acevedo Aug 30 '18 at 14:13
  • The link which u given is for twilio account authentication. The **AUTHY_ID** will be for the twilio account holder is it right ? – prasanthi Aug 31 '18 at 07:48
0
        #region Namespaces
        using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Web;
        using System.Data;
        using System.Data.SqlClient;
        using System.Data.Sql;
        using System.Data.SqlTypes;
        using System.Configuration;
        using System.Web.UI.WebControls;
        using System.Net;
        using System.IO;
        #endregion
        
        /// <summary>
        /// Summary description for SMSAPI
        /// </summary>
        public class SMSAPI
        {
        
            MyConnection con = new MyConnection();
            public DataSet ds = new DataSet();
            public SqlDataAdapter da = new SqlDataAdapter();
            public SMSAPI()
            {
                //
                // TODO: Add constructor logic here
                //
            }
        
        
            /*Get The SMS API*/
            public string GETSMSAPI()
            {
        
                con.Open();
                string QuerySMS, StringSMS, APISMS, UserName, Password, Sender, Priority;
                QuerySMS = "SP_SMSAPIMaster_GetDetail";
                StringSMS = APISMS = UserName = Password = Sender = Priority = "";
                con.cmd.CommandText = QuerySMS;
                try
                {
                    con.Open();
                    con.cmd.ExecuteNonQuery();
                    SqlDataAdapter da = new SqlDataAdapter(con.cmd);
                    da.Fill(ds, "tbl_SMSAPIMaster");
        
                    if (ds.Tables["tbl_SMSAPIMaster"].Rows.Count > 0)
                    {
                        APISMS = ds.Tables["tbl_SMSAPIMaster"].Rows[0]["SMSAPI"].ToString();
                        UserName = ds.Tables["tbl_SMSAPIMaster"].Rows[0]["UserName"].ToString();
                        Password = ds.Tables["tbl_SMSAPIMaster"].Rows[0]["Password"].ToString();
                        Sender = ds.Tables["tbl_SMSAPIMaster"].Rows[0]["Sender"].ToString();
                        Priority = ds.Tables["tbl_SMSAPIMaster"].Rows[0]["Priority"].ToString();
                        StringSMS = APISMS.Replace("uid", UserName).Replace("psd", Password).Replace("sndid", Sender).Replace("prt", Priority);
                    }
                    else
                    {
                        StringSMS = "";
                    }
                }
                catch
                {
                }
                finally
                {
                    con.Close();
                }
                
                return StringSMS;
        
            }
            /*Call The SMS API*/
            public string GetAPICALL(string url)
            {
                HttpWebRequest httpreq = (HttpWebRequest)WebRequest.Create(url);
                try
                {
                    HttpWebResponse httpres = (HttpWebResponse)httpreq.GetResponse();
                    StreamReader sr = new StreamReader(httpres.GetResponseStream());
                    string results = sr.ReadToEnd();
                    sr.Close();
                    return results;
                }
                catch
                {
                    return "0";
                }
            }
            
        }
    
     private void SendOTPSMS()
        {
            string Msgs, SMSString;
            Msgs = SMSString = "";
            SMSString = SMSAPI.GETSMSAPI();
            Msgs = "D" + "OTP : " + lblOneTimePassword;
            lblMessageContent = Msgs;
            SMSString = SMSString.Replace("num", txtMobileNo.Value.Trim()).Replace("fedmesge", Msgs);
            string Result = SMSAPI.GetAPICALL(SMSString);
        }


  private void GenerateOTP()
    {
        int MaxSize = 4;
        //int MinSize = 0;
        char[] chars = new char[62];
        string Character;
        // a = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
        Character = "1234567890";
        chars = Character.ToCharArray();
        int Size = MaxSize;
        byte[] data = new byte[1];
        RNGCryptoServiceProvider crypto = new RNGCryptoServiceProvider();
        crypto.GetNonZeroBytes(data);
        Size = MaxSize;
        data = new byte[Size];
        crypto.GetNonZeroBytes(data);
        StringBuilder result = new StringBuilder(Size);
        foreach (byte b in data)
        {
            result.Append(chars[b % (chars.Length)]);
        }

        lblOneTimePassword = result.ToString();

    }
Code
  • 679
  • 5
  • 9