Esempio n. 1
0
        /// <summary>
        /// 发送短信验证码
        /// </summary>
        /// <param name="smsCode"></param>
        /// <returns></returns>
        public async Task<ResponseData<SendSmsResult>> SendSmsCodeAsync(SendCloudSmsCode smsCode)
        {
            if (smsCode == null) throw new ArgumentNullException(nameof(smsCode));
            if (string.IsNullOrWhiteSpace(this.Configuration.SmsUser)) throw new ArgumentNullException(nameof(this.Configuration.SmsUser));
            if (string.IsNullOrWhiteSpace(this.Configuration.SmsKey)) throw new ArgumentNullException(nameof(this.Configuration.SmsKey));
            Contract.EndContractBlock();

            smsCode.Validate();

            var timestampResponse = await this.GetServerTimeStampAsync();
            var timestamp = timestampResponse.Info.TimeStamp;

            var credential = new Credential(this.Configuration.SmsUser, this.Configuration.SmsKey);
            var treeMap = new SortedDictionary<string, string>();
            treeMap.Add("smsUser", credential.ApiUser);
            treeMap.Add("code", smsCode.Code);
            treeMap.Add("phone", smsCode.Phone);
            treeMap.Add("timestamp", timestamp.ToString());
            if (smsCode.LabelId != null)
            {
                treeMap.Add("labelId", smsCode.LabelId.Value.ToString());
            }

            var signature = Md5Utils.MD5Signature(treeMap, credential.ApiKey);
            treeMap.Add("signature", signature);

            using (var client = new HttpClient())
            {
                var response = await client.PostAsync(this.Configuration.SendSmsApi, new FormUrlEncodedContent(treeMap));
                return await this.ValidateAsync<SendSmsResult>(response);
            }
        }
Esempio n. 2
0
        public async void SendCodeTest()
        {
            var code = new SendCloudSmsCode {
                Phone = "",
                Code  = "311920"
            };

            var response = await _client.SendCodeAsync(code);

            Assert.True(response.StatusCode == 200, JsonConvert.SerializeObject(response));
        }
Esempio n. 3
0
        public async Task <ResponseData <SmsCalledResult> > SendCodeAsync(SendCloudSmsCode code)
        {
            if (code == null)
            {
                throw new ArgumentNullException(nameof(code));
            }
            if (string.IsNullOrWhiteSpace(_sendCloudAccount.SmsUser))
            {
                throw new ArgumentNullException(nameof(_sendCloudAccount.SmsUser));
            }
            if (string.IsNullOrWhiteSpace(_sendCloudAccount.SmsKey))
            {
                throw new ArgumentNullException(nameof(_sendCloudAccount.SmsKey));
            }

            code.CheckParameters();

            var timestamp = await GetTimeStampAsync();

            var bizParams = new SortedDictionary <string, string>
            {
                { "smsUser", _sendCloudAccount.SmsUser },
                { "code", code.Code },
                { "phone", code.Phone },
                { "timestamp", timestamp.Info.Timestamp.ToString() }
            };

            if (code.LabelId != null)
            {
                bizParams.Add("labelId", code.LabelId.Value.ToString());
            }

            var signature = SignatureHelper.GetApiSignature(bizParams, _sendCloudAccount.SmsKey);

            bizParams.Add("signature", signature);

            var content = new FormUrlEncodedContent(bizParams);

            return(await _proxy.SendCodeAsync(content)
                   .Retry(_config.RetryTimes)
                   .Handle().WhenCatch <Exception>(e =>
            {
                _exceptionHandler?.Invoke(e);
                return ReturnAsDefautlResponse();
            }));
        }