Ejemplo n.º 1
0
        /// <summary>
        /// 阿里云发送短信验证码
        /// </summary>
        /// <param name="mobile">手机号</param>
        /// <param name="jsonData">Json数据</param>
        /// <param name="templateCode">templateCode</param>
        /// <returns></returns>
        private async Task <ApiResult> SendAsync(string mobile, string jsonData, string templateCode)
        {
            if (!ValidateHelper.IsCellPhone(mobile))
            {
                return(ApiResult.ValidateFail());
            }

            if (!_apiEnabled)
            {
                return(ApiResult.Error("管理员禁用短信发送Api"));
            }

            string EndPoint        = _config["SMSAli:EndPoint"];
            string AccessKeyId     = _config["SMSAli:AccessKeyId"];
            string AccessKeySecret = _config["SMSAli:AccessKeySecret"];
            string SignName        = _config["SMSAli:SignName"];

            if (SignName.IsNull())
            {
                return(ApiResult.Error("管理员未配置短信发送相关参数"));
            }

            string nowDate = DateTime.Now.ToUniversalTime().ToString("yyyy-MM-dd'T'HH:mm:ss'Z'"); //GTM时间
            Dictionary <string, string> keyValues = new Dictionary <string, string>();            //声明一个字典

            //1.系统参数
            keyValues.Add("SignatureMethod", "HMAC-SHA1");
            keyValues.Add("SignatureNonce", Guid.NewGuid().ToString());
            keyValues.Add("AccessKeyId", AccessKeyId);
            keyValues.Add("SignatureVersion", "1.0");
            keyValues.Add("Timestamp", nowDate);
            keyValues.Add("Format", "Json");//可换成xml

            //2.业务api参数
            keyValues.Add("Action", "SendSms");
            keyValues.Add("Version", "2017-05-25");
            keyValues.Add("RegionId", "cn-hangzhou");
            keyValues.Add("PhoneNumbers", mobile);
            keyValues.Add("SignName", SignName);
            keyValues.Add("TemplateParam", jsonData);
            keyValues.Add("TemplateCode", templateCode);
            keyValues.Add("OutId", "");

            //3.去除签名关键字key
            if (keyValues.ContainsKey("Signature"))
            {
                keyValues.Remove("Signature");
            }

            //4.参数key排序
            Dictionary <string, string> ascDic = keyValues.OrderBy(o => o.Key).ToDictionary(o => o.Key, p => p.Value.ObjToString());
            //System.Diagnostics.Trace.WriteLine(">>>>>>>>>>>>>>>>>>"+JsonHelper.Serialize(ascDic));
            //5.构造待签名的字符串
            StringBuilder builder = new StringBuilder();

            foreach (var item in ascDic)
            {
                if (item.Key == "SignName")
                {
                }
                else
                {
                    builder.Append('&').Append(specialUrlEncode(item.Key)).Append('=').Append(specialUrlEncode(item.Value));
                }
                if (item.Key == "RegionId")
                {
                    builder.Append('&').Append(specialUrlEncode("SignName")).Append('=').Append(specialUrlEncode(keyValues["SignName"]));
                }
            }
            string sorteQueryString = builder.ToString().Substring(1);

            StringBuilder stringToSign = new StringBuilder();

            stringToSign.Append("GET").Append('&');
            stringToSign.Append(specialUrlEncode("/")).Append('&');
            stringToSign.Append(specialUrlEncode(sorteQueryString));

            string Sign = MySign(AccessKeySecret + "&", stringToSign.ToString());
            //6.签名最后也要做特殊URL编码
            string signture = specialUrlEncode(Sign);

            //最终打印出合法GET请求的URL
            string url    = string.Format("http://{0}/?Signature={1}{2}", EndPoint, signture, builder);
            var    result = await _httpService.GetAsync(url);

            if (result.IsNull())
            {
                _logger.LogError($"【阿里云SMS】手机号:{mobile},模板内容:{jsonData},返回结果为空");
            }
            _logger.LogInformation($"【阿里云SMS】手机号:{mobile},模板内容:{jsonData},返回结果:{result}");
            if (result == "OK")
            {
                return(ApiResult.OK("OK", jsonData));
            }
            else
            {
                return(ApiResult.ValidateFail(result, jsonData));
            }
        }