1 #region 驗證碼相關 2 public class SmsSDK 3 { 4 5 #region 短信發送的必要參數 6 /// <summary> 7 /// appId 8 /// </summary> 9 private static int appId =本身的; 10 11 /// <summary> 12 /// appKey 13 /// </summary> 14 private static string appKey = "本身的"; 15 16 /// <summary> 17 /// 短信模板ID 18 /// </summary> 19 private static int tmplateId =本身的; 20 21 #endregion 22 23 #region 接收驗證碼的手機號 24 /// <summary> 25 /// 接受手機號 26 /// </summary> 27 private static string phoneNumber = ""; 28 #endregion 29 30 #region 獲取驗證碼 31 /// <summary> 32 /// 獲取驗證碼 33 /// </summary> 34 /// <param name="args"></param> 35 /// <param name="phone">手機號</param> 36 /// <returns></returns> 37 public bool GetMain(string phone) 38 { 39 var tel = phone; 40 phoneNumber = tel; 41 SmsSingleSenderResult singleResult; 42 List<string> templParams = new List<string>(); 43 SmsSingleSender singleSender = new SmsSingleSender(appId, appKey); 44 //獲取隨機碼 45 var val = new SmsSenderUtil().GetRandom().ToString(); 46 SixNums = val; 47 48 templParams.Add(val); 49 templParams.Add("10"); 50 singleResult = singleSender.SendWithParam(phoneNumber, tmplateId, templParams); 51 if (singleResult.result == 0) 52 { 53 return true; 54 } 55 return false; 56 57 } 58 #endregion
1 #region 判斷驗證碼 2 /// <summary> 3 /// 隨機碼 4 /// </summary> 5 private static string SixNums = ""; 6 7 /// <summary> 8 /// 判斷驗證碼 9 /// </summary> 10 /// <param name="IfgCode">返回填寫的驗證碼</param> 11 /// <returns></returns> 12 public bool JudgeIdentifyingCode(string IfgCode) 13 { 14 if (SixNums != IfgCode) 15 { 16 return false; 17 } 18 return true; 19 } 20 #endregion 21 } 22 #endregion 23 24 #region 騰訊雲配置 25 #region 短信類型枚舉 26 /// <summary> 27 /// 短信類型枚舉 28 /// </summary> 29 public enum SmsType 30 { 31 普通短信 = 0, 32 營銷短信 = 1 33 } 34 #endregion 35 36 #region 短信單發 37 /// <summary> 38 /// 單發 39 /// </summary> 40 class SmsSingleSender 41 { 42 #region 變量 43 /// <summary> 44 /// appId 45 /// </summary> 46 private int appId; 47 /// <summary> 48 /// appkey 49 /// </summary> 50 private string appkey; 51 /// <summary> 52 /// url 53 /// </summary> 54 private string url = "https://yun.tim.qq.com/v5/tlssmssvr/sendsms"; 55 /// <summary> 56 /// util 57 /// </summary> 58 private SmsSenderUtil util = new SmsSenderUtil(); 59 #endregion 60 61 #region 構造 62 /// <summary> 63 /// 構造函數 64 /// </summary> 65 /// <param name="sdkappid"></param> 66 /// <param name="appkey"></param> 67 public SmsSingleSender(int sdkappid, string appkey) 68 { 69 this.appId = sdkappid; 70 this.appkey = appkey; 71 } 72 #endregion 73 74 75 #region 指定模板單發 76 /// <summary> 77 /// 指定模板單發 78 /// </summary> 79 /// <param name="phoneNumber">不帶國家碼的手機號</param> 80 /// <param name="templId">模板 id</param> 81 /// <param name="templParams">模板參數列表,如模板 {1}...{2}...{3},那麼須要帶三個參數</param> 82 /// <param name="sign">短信簽名,若是使用默認簽名,該字段可缺省</param> 83 /// <param name="extend">擴展碼,可填空</param> 84 /// <param name="ext">服務端原樣返回的參數,可填空</param> 85 /// <returns>SmsSingleSenderResult</returns> 86 public SmsSingleSenderResult SendWithParam(string phoneNumber, int templId, List<string> templParams, string sign= "", string extend = "", string ext = "") 87 { 88 long random = util.GetRandom(); 89 long curTime = util.GetCurTime(); 90 // 按照協議組織 post 請求包體 91 JObject tel = new JObject(); 92 tel.Add("nationcode", SmsSenderUtil.nationCode); 93 tel.Add("mobile", phoneNumber); 94 JObject data = new JObject(); 95 data.Add("tel", tel); 96 data.Add("sig", util.CalculateSigForTempl(appkey, random, curTime, phoneNumber)); 97 data.Add("tpl_id", templId); 98 data.Add("params", util.SmsParamsToJSONArray(templParams)); 99 data.Add("sign", sign); 100 data.Add("time", curTime); 101 data.Add("extend", extend); 102 data.Add("ext", ext); 103 string wholeUrl = url + "?sdkappid=" + appId + "&random=" + random; 104 HttpWebRequest request = util.GetPostHttpConn(wholeUrl); 105 byte[] requestData = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(data)); 106 request.ContentLength = requestData.Length; 107 Stream requestStream = request.GetRequestStream(); 108 requestStream.Write(requestData, 0, requestData.Length); 109 requestStream.Close(); 110 // 接收返回包 111 HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 112 Stream responseStream = response.GetResponseStream(); 113 StreamReader streamReader = new StreamReader(responseStream, Encoding.GetEncoding("utf-8")); 114 string responseStr = streamReader.ReadToEnd(); 115 streamReader.Close(); 116 responseStream.Close(); 117 SmsSingleSenderResult result; 118 if (HttpStatusCode.OK == response.StatusCode) 119 { 120 result = util.ResponseStrToSingleSenderResult(responseStr); 121 } 122 else 123 { 124 result = new SmsSingleSenderResult(); 125 result.result = -1; 126 result.errmsg = "http error " + response.StatusCode + " " + responseStr; 127 } 128 return result; 129 } 130 #endregion 131 } 132 #endregion
1 #region 單髮結果 2 /// <summary> 3 /// 單髮結果 4 /// </summary> 5 public class SmsSingleSenderResult 6 { 7 /// <summary> 8 /// 錯誤碼,0 表示成功(計費依據),非 0 表示失敗 9 /// </summary> 10 public int result { set; get; } 11 /// <summary> 12 /// 錯誤消息,result 非 0 時的具體錯誤信息 13 /// </summary> 14 public string errmsg { set; get; } 15 /// <summary> 16 /// 用戶的 session 內容,騰訊 server 回包中會原樣返回 17 /// </summary> 18 public string ext { set; get; } 19 /// <summary> 20 /// 本次發送標識 id,標識一次短信下發記錄 21 /// </summary> 22 public string sid { set; get; } 23 /// <summary> 24 /// 短信計費的條數 25 /// </summary> 26 public int fee { set; get; } 27 /// <summary> 28 /// ToString() 29 /// </summary> 30 /// <returns></returns> 31 public override string ToString() 32 { 33 return string.Format("SmsSingleSenderResult\nresult {0}\nerrMsg {1}\next {2}\nsid {3}\nfee {4}", result, errmsg, ext, sid, fee); 34 } 35 } 36 #endregion 37 38 #region 公共類 39 /// <summary> 40 /// 公共類 41 /// </summary> 42 class SmsSenderUtil 43 { 44 /// <summary> 45 /// 國家碼 46 /// </summary> 47 public static string nationCode = "86"; 48 /// <summary> 49 /// 隨機數生成器 50 /// </summary> 51 private Random random = new Random(); 52 53 #region GetPostHttpConn 54 /// <summary> 55 /// GetPostHttpConn 56 /// </summary> 57 /// <param name="url"></param> 58 /// <returns></returns> 59 public HttpWebRequest GetPostHttpConn(string url) 60 { 61 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); 62 request.Method = "POST"; 63 request.ContentType = "application/x-www-form-urlencoded"; 64 return request; 65 } 66 #endregion
1 #region 生成隨機數 2 /// <summary> 3 /// 生成隨機數 4 /// </summary> 5 /// <returns></returns> 6 public long GetRandom() 7 { 8 return random.Next(999999) % 900000 + 100000; 9 } 10 #endregion 11 12 #region 獲取請求發起時間 13 /// <summary> 14 /// 獲取請求發起時間, 15 /// unix 時間戳(單位:秒),若是和系統時間相差超過 10 分鐘則會返回失敗 16 /// </summary> 17 /// <returns></returns> 18 public long GetCurTime() 19 { 20 Int32 unixTimestamp = (Int32)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds; 21 return unixTimestamp; 22 } 23 #endregion 24 25 #region 字符串轉SHA256 26 /// <summary> 27 /// 字符串轉SHA256 28 /// </summary> 29 /// <param name="str"></param> 30 /// <returns></returns> 31 public string StrToHash(string str) 32 { 33 SHA256 sha256 = SHA256Managed.Create(); 34 byte[] resultByteArray = sha256.ComputeHash(System.Text.Encoding.UTF8.GetBytes(str)); 35 return ByteArrayToHex(resultByteArray); 36 } 37 38 /// <summary> 39 /// 將二進制的數值轉換爲 16 進制字符串,如 "abc" => "616263" 40 /// </summary> 41 /// <param name="byteArray"></param> 42 /// <returns></returns> 43 private static string ByteArrayToHex(byte[] byteArray) 44 { 45 string returnStr = ""; 46 if (byteArray != null) 47 { 48 for (int i = 0; i < byteArray.Length; i++) 49 { 50 returnStr += byteArray[i].ToString("x2"); 51 } 52 } 53 return returnStr; 54 } 55 #endregion 56 57 #region 將單發回包解析成結果對象 58 /// <summary> 59 /// 將單發回包解析成結果對象 60 /// </summary> 61 /// <param name="str"></param> 62 /// <returns></returns> 63 public SmsSingleSenderResult ResponseStrToSingleSenderResult(string str) 64 { 65 SmsSingleSenderResult result = JsonConvert.DeserializeObject<SmsSingleSenderResult>(str); 66 return result; 67 } 68 #endregion 69 70 #region List<string>轉JArray 71 /// <summary> 72 /// List<string>轉JArray 73 /// </summary> 74 /// <param name="templParams"></param> 75 /// <returns></returns> 76 public JArray SmsParamsToJSONArray(List<string> templParams) 77 { 78 JArray smsParams = new JArray(); 79 foreach (string templParamsElement in templParams) 80 { 81 smsParams.Add(templParamsElement); 82 } 83 return smsParams; 84 } 85 #endregion 86 87 #region PhoneNumbersToJSONArray 88 /// <summary> 89 /// PhoneNumbersToJSONArray 90 /// </summary> 91 /// <param name="nationCode"></param> 92 /// <param name="phoneNumbers"></param> 93 /// <returns></returns> 94 public JArray PhoneNumbersToJSONArray(string nationCode, List<string> phoneNumbers) 95 { 96 JArray tel = new JArray(); 97 int i = 0; 98 do 99 { 100 JObject telElement = new JObject(); 101 telElement.Add("nationcode", nationCode); 102 telElement.Add("mobile", phoneNumbers.ElementAt(i)); 103 tel.Add(telElement); 104 } while (++i < phoneNumbers.Count); 105 return tel; 106 } 107 #endregion 108 109 #region 計算App憑證 110 /* 111 "sig" 字段根據公式 sha256(appkey=$appkey&random=$random&time=$time&mobile=$mobile)生成 112 */ 113 public string CalculateSigForTempl(string appkey, long random, long curTime, List<string> phoneNumbers) 114 { 115 string phoneNumbersString = phoneNumbers.ElementAt(0); 116 for (int i = 1; i < phoneNumbers.Count; i++) 117 { 118 phoneNumbersString += "," + phoneNumbers.ElementAt(i); 119 } 120 return StrToHash(String.Format( 121 "appkey={0}&random={1}&time={2}&mobile={3}", 122 appkey, random, curTime, phoneNumbersString)); 123 } 124 125 public string CalculateSigForTempl(string appkey, long random, long curTime, string phoneNumber) 126 { 127 List<string> phoneNumbers = new List<string>(); 128 phoneNumbers.Add(phoneNumber); 129 return CalculateSigForTempl(appkey, random, curTime, phoneNumbers); 130 } 131 132 public string CalculateSig(string appkey, long random, long curTime, List<string> phoneNumbers) 133 { 134 string phoneNumbersString = phoneNumbers.ElementAt(0); 135 for (int i = 1; i < phoneNumbers.Count; i++) 136 { 137 phoneNumbersString += "," + phoneNumbers.ElementAt(i); 138 } 139 return StrToHash(String.Format( 140 "appkey={0}&random={1}&time={2}&mobile={3}", 141 appkey, random, curTime, phoneNumbersString)); 142 } 143 #endregion 144 } 145 #endregion 146 #endregion
//使用時須要將配置裏的內容換成本身的;session