Exemplo n.º 1
0
        /// <summary>
        /// 发送验证码
        /// </summary>
        /// <param name="phone">The phone.</param>
        /// <returns>Task&lt;Response&lt;System.Boolean&gt;&gt;.</returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task <Response <bool> > SendIdentifyingCodeAsync(SendCodeRequest request)
        {
            var cacheKey = $"sms__{request.Cphone}";

            if (this.memory.TryGetValue <CachedIdentifyingCode>(cacheKey, out CachedIdentifyingCode cacheCode))
            {
                if (cacheCode.LastTime.HasValue && (DateTime.Now - cacheCode.LastTime.Value).TotalSeconds < 30)
                {
                    throw new Exception("规定时间内不能重复获取验证码");
                }
            }
            else
            {
                cacheCode = new CachedIdentifyingCode()
                {
                    Code = new Random().Next(1000, 9999).ToString()
                };
                this.memory.Set(cacheKey, cacheCode, DateTimeOffset.Now.AddSeconds(300));//验证码缓存60秒
            }
            string message   = $"您好,您的验证码:{cacheCode.Code},请及时输入,五分钟之内有效";
            bool   isSuccess = await this._SendMessage2Phone(request.Cphone, message, cacheCode.Code);

            if (isSuccess)
            {
                cacheCode.LastTime = DateTime.Now;
            }
            return(new Response <bool>
            {
                Data = isSuccess
            });
        }
Exemplo n.º 2
0
 public Task <Response <bool> > SendIdentifyingCode([FromBody] SendCodeRequest request)
 {
     return(this.userService.SendIdentifyingCodeAsync(request));
 }
Exemplo n.º 3
0
        public void sendCodeRequest()
        {
            var sendCodeRequest = new SendCodeRequest
            {
                mocean_brand           = "test brand",
                mocean_code_length     = "test codelength",
                mocean_from            = "test from",
                mocean_next_event_wait = "test nexteventwait",
                mocean_pin_validity    = "test pinvalidity",
                mocean_template        = "test template",
                mocean_to          = "test to",
                mocean_reqid       = "test req id",
                mocean_request_nl  = "test request nl",
                mocean_resp_format = "json"
            };

            Assert.IsNotNull(sendCodeRequest.mocean_brand);
            Assert.AreEqual(sendCodeRequest.mocean_brand, "test brand");
            Assert.IsNotNull(sendCodeRequest.mocean_code_length);
            Assert.AreEqual(sendCodeRequest.mocean_code_length, "test codelength");
            Assert.IsNotNull(sendCodeRequest.mocean_from);
            Assert.AreEqual(sendCodeRequest.mocean_from, "test from");
            Assert.IsNotNull(sendCodeRequest.mocean_next_event_wait);
            Assert.AreEqual(sendCodeRequest.mocean_next_event_wait, "test nexteventwait");
            Assert.IsNotNull(sendCodeRequest.mocean_pin_validity);
            Assert.AreEqual(sendCodeRequest.mocean_pin_validity, "test pinvalidity");
            Assert.IsNotNull(sendCodeRequest.mocean_template);
            Assert.AreEqual(sendCodeRequest.mocean_template, "test template");
            Assert.IsNotNull(sendCodeRequest.mocean_to);
            Assert.AreEqual(sendCodeRequest.mocean_to, "test to");
            Assert.IsNotNull(sendCodeRequest.mocean_reqid);
            Assert.AreEqual(sendCodeRequest.mocean_reqid, "test req id");
            Assert.IsNotNull(sendCodeRequest.mocean_request_nl);
            Assert.AreEqual(sendCodeRequest.mocean_request_nl, "test request nl");
            Assert.IsNotNull(sendCodeRequest.mocean_resp_format);
            Assert.AreEqual(sendCodeRequest.mocean_resp_format, "json");

            sendCodeRequest = new SendCodeRequest();
            Assert.IsNull(sendCodeRequest.mocean_brand);
            Assert.IsNull(sendCodeRequest.mocean_code_length);
            Assert.IsNull(sendCodeRequest.mocean_from);
            Assert.IsNull(sendCodeRequest.mocean_next_event_wait);
            Assert.IsNull(sendCodeRequest.mocean_pin_validity);
            Assert.IsNull(sendCodeRequest.mocean_template);
            Assert.IsNull(sendCodeRequest.mocean_to);
            Assert.IsNull(sendCodeRequest.mocean_reqid);
            Assert.IsNull(sendCodeRequest.mocean_request_nl);
            Assert.IsNull(sendCodeRequest.mocean_resp_format);
            sendCodeRequest.mocean_brand = "test brand";
            Assert.AreEqual(sendCodeRequest.mocean_brand, "test brand");
            sendCodeRequest.mocean_code_length = "test codelength";
            Assert.AreEqual(sendCodeRequest.mocean_code_length, "test codelength");
            sendCodeRequest.mocean_from = "test from";
            Assert.AreEqual(sendCodeRequest.mocean_from, "test from");
            sendCodeRequest.mocean_next_event_wait = "test nexteventwait";
            Assert.AreEqual(sendCodeRequest.mocean_next_event_wait, "test nexteventwait");
            sendCodeRequest.mocean_pin_validity = "test pinvalidity";
            Assert.AreEqual(sendCodeRequest.mocean_pin_validity, "test pinvalidity");
            sendCodeRequest.mocean_template = "test template";
            Assert.AreEqual(sendCodeRequest.mocean_template, "test template");
            sendCodeRequest.mocean_to = "test to";
            Assert.AreEqual(sendCodeRequest.mocean_to, "test to");
            sendCodeRequest.mocean_reqid = "test req id";
            Assert.AreEqual(sendCodeRequest.mocean_reqid, "test req id");
            sendCodeRequest.mocean_request_nl = "test request nl";
            Assert.AreEqual(sendCodeRequest.mocean_request_nl, "test request nl");
            sendCodeRequest.mocean_resp_format = "json";
            Assert.AreEqual(sendCodeRequest.mocean_resp_format, "json");
        }