Пример #1
0
        /// <summary>
        ///     生成验证码
        /// </summary>
        /// <param name="projectId"></param>
        /// <param name="mobile"></param>
        /// <returns></returns>
        public long GenerateVerifiyCode(string mobile)
        {
            //var messageAccount = Ioc.Resolve<IMessageAccountService>().GetSingle(projectId);
            //if (messageAccount == null) return 0;
            var cacheKey = _CodeCacheKey + $"{mobile}";

            ObjectCache.Remove(cacheKey);
            var code        = new Random().Next(100001, 999999);
            var verifiyCode = new VerifiyCode {
                Code       = code,
                CreateTime = DateTime.Now,
                Mobile     = mobile
            };

            ObjectCache.Set(cacheKey, verifiyCode);
            return(verifiyCode.Code);
        }
Пример #2
0
        /// <summary>
        ///     核对验证码
        /// </summary>
        /// <param name="projectId"></param>
        /// <param name="mobile"></param>
        /// <returns></returns>
        public bool CheckVerifiyCode(string mobile, long code)
        {
            VerifiyCode verifiyCodeRes = null;
            var         cacheKey       = _CodeCacheKey + $"{mobile}";

            if (ObjectCache.TryGet(cacheKey, out VerifiyCode verifiyCode))
            {
                verifiyCodeRes = verifiyCode;
                if (verifiyCode.Code == code)
                {
                    ObjectCache.Remove(cacheKey);
                    if (verifiyCode.CreateTime > DateTime.Now.AddMinutes(-30))
                    {
                        return(true);
                    }
                }
            }

            return(false);
        }