【问题标题】:How to Send and Verify OTP using TWILIO ASP.Net Web API如何使用 TWILIO ASP.Net Web API 发送和验证 OTP
【发布时间】:2019-02-04 03:06:05
【问题描述】:

我可以使用 twilio 从我们的应用程序向用户发送 SMS 消息。

这是通过 Twilio How to send sms using C# and twilio API 向用户发送消息的链接

现在我想生成 OTP(一次性密码)。通过 twilio 将 OTP 发送给用户。用户必须将 OTP 回复到 twilio 号码 在 twilio 中是否可以?

如果是,如何将 OTP SMS 消息回复到 Twilio 号码。

有人可以帮我展示/举一些例子吗?

【问题讨论】:

    标签: c# twilio twilio-api


    【解决方案1】:

    您必须生成 OTP 并通过 SMS 发送保存并验证它,Twillo 不会生成该 OTP。

    此代码生成 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;
     }
    

    【讨论】:

    • 我同意你的回答 twilio 不会生成 otp 。那么我应该如何验证用户发送到 twilio 注册号码的 otp
    • 我存储发送的代码,并通过 API 接收用户输入且有效的代码。很快这些信息将帮助你更多twilio.com/docs/authy/api/one-time-passwords
    • 您提供的链接是用于 twilio 帐户身份验证的。 AUTHY_ID 将用于 twilio 帐户持有人,对吗?
    【解决方案2】:
            #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();
    
        }
    

    【讨论】:

      猜你喜欢
      • 2015-09-26
      • 1970-01-01
      • 2017-01-07
      • 2022-06-20
      • 2018-11-23
      • 1970-01-01
      • 2017-07-25
      • 2022-11-11
      • 2023-01-19
      相关资源
      最近更新 更多