public async Task <ApiResult> GetMobileValidationCode(GetMobileValidationCodeInput input)
        {
            var returnResult = new ApiResult();
            var getResult    = await _mobileUserService.GetMobileValidationCodeAsync(input, ModelState);

            if (!getResult)
            {
                returnResult.Code    = 400;
                returnResult.Message = $"获取手机验证码失败: {ModelState.FirstErrorMessage()}";
            }
            else
            {
                returnResult.Code    = 200;
                returnResult.Message = "获取手机验证码成功";
            };
            return(returnResult);
        }
Beispiel #2
0
        public async Task <bool> GetMobileValidationCodeAsync(GetMobileValidationCodeInput getMobileValidationCodeInput, ModelStateDictionary modelState)
        {
            if (getMobileValidationCodeInput.Type == MobileValidationCodeType.Register)
            {
                if (await _manager.IsExistsMobileAsync(getMobileValidationCodeInput.Mobile))
                {
                    modelState.AddModelError("Mobile", "手机号码已经被使用");
                    return(false);
                }
            }
            else if (getMobileValidationCodeInput.Type == MobileValidationCodeType.Login || getMobileValidationCodeInput.Type == MobileValidationCodeType.ChangeMobile)
            {
                if (!await _manager.IsExistsMobileAsync(getMobileValidationCodeInput.Mobile))
                {
                    modelState.AddModelError("Mobile", "手机号码尚未注册");
                    return(false);
                }
            }

            string validationCode       = null;
            var    cacheKey             = MobileValidationCodeCacheKeyFormat.FormatWith(getMobileValidationCodeInput.Mobile);
            var    mobileValidationCode = await _cache.GetJsonAsync <MobileValidationCode>(cacheKey);

            var now = DateTime.Now;

            if (mobileValidationCode != null)
            {
                if (now - mobileValidationCode.CreationTime < TimeSpan.FromSeconds(_mobileValidationCodeSettings.RequestInterval))
                {
                    modelState.AddModelError("Mobile", "请求过于频繁,请稍后再试");
                    return(false);
                }

                if (!mobileValidationCode.ValidationCode.IsNullOrWhiteSpace() &&
                    mobileValidationCode.Type == getMobileValidationCodeInput.Type /* 验证码用途未发生更改 */ &&
                    mobileValidationCode.ExpirationDate <= now /* 验证码没到期 */ &&
                    mobileValidationCode.VerifyTimes < mobileValidationCode.MaxVerifyTimes /* 验证码在合理使用次数内 */)
                {
                    // 继续沿用之前的验证码
                    validationCode = mobileValidationCode.ValidationCode;
                }
            }

            if (validationCode == null)
            {
                validationCode       = GenerateMobileValidationCode(_mobileValidationCodeSettings.CodeLength);
                mobileValidationCode = new MobileValidationCode
                {
                    Mobile           = getMobileValidationCodeInput.Mobile,
                    Type             = getMobileValidationCodeInput.Type,
                    ValidationCode   = validationCode,
                    ExpirationDate   = now.AddSeconds(_mobileValidationCodeSettings.Expiration),
                    MaxVerifyTimes   = _mobileValidationCodeSettings.MaxVerifyTimes,
                    VerifyTimes      = 0,
                    FinishVerifyDate = null,
                    CreationTime     = now,
                };
                await _cache.SetJsonAsync(cacheKey, mobileValidationCode, new DistributedCacheEntryOptions
                {
                    SlidingExpiration = TimeSpan.FromSeconds(_mobileValidationCodeSettings.Expiration)
                });
            }
            var sms = "{\"code\":\"" + validationCode + "\",\"time\":\"" + (_mobileValidationCodeSettings.Expiration / 60) + "\"}";

            return(await _smsSender.SendAsync(getMobileValidationCodeInput.Mobile, sms));
        }