Exemple #1
0
        /// <summary>
        /// 发送短信
        /// </summary>
        internal async Task <(bool success, string response)> Send(SmsObject sms)
        {
            var paramers = new Dictionary <string, string>();

            paramers.Add("PhoneNumbers", sms.Mobile);
            paramers.Add("SignName", sms.Signature);
            paramers.Add("TemplateCode", sms.TempletKey);
            paramers.Add("TemplateParam", JsonConvert.SerializeObject(sms.Data));
            paramers.Add("OutId", sms.OutId);
            paramers.Add("AccessKeyId", AccessKeyId);

            try
            {
                string url = GetSignUrl(paramers, AccessKeySecret);

                int retryTimes = 1;
                var reply      = await HttpGetAsync(url);

                while (500 <= reply.StatusCode && AutoRetry && retryTimes < MaxRetryNumber)
                {
                    url   = GetSignUrl(paramers, AccessKeySecret);
                    reply = await HttpGetAsync(url);

                    retryTimes++;
                }

                if (!string.IsNullOrEmpty(reply.response))
                {
                    var res = JsonConvert.DeserializeObject <Dictionary <string, string> >(reply.response);
                    if (res != null && res.ContainsKey("Code") && "OK".Equals(res["Code"]))
                    {
                        return(true, response : reply.response);
                    }
                }

                return(false, response : reply.response);
            }
            catch (Exception ex)
            {
                return(false, response : ex.Message);
            }
        }
Exemple #2
0
        /// <summary>
        /// 发送短信验证码
        /// </summary>
        /// <returns></returns>
        public async Task <(bool success, string response)> SendSmsCode(string phone, string code)
        {
            try
            {
                IDictionary <string, string> data = new Dictionary <string, string>();
                data.Add("code", code);
                var sms = new SmsObject
                {
                    Mobile     = phone,
                    Signature  = _smsOptions.SmsSignature,
                    TempletKey = _smsOptions.SmsTempletKey,
                    Data       = data,
                    OutId      = "OutId"
                };

                return(await new AliyunSms(_aliyunOptions.AccessKeyId, _aliyunOptions.AccessKeySecret).Send(sms));
            }
            catch
            {
                return(false, response : "");
            }
        }