Exemple #1
0
        public bool ExistsKey(string phone)
        {
            if (string.IsNullOrEmpty(phone))
            {
                return(false);
            }

            lock (KeyLocker)
            {
                var cacheKey  = BuildCacheKey(phone);
                var phoneKeys = KeyCache.Get <Dictionary <string, DateTime> >(cacheKey);
                return(phoneKeys != null);
            }
        }
Exemple #2
0
        public Result ValidateKey(string phone, string key)
        {
            key = (key ?? string.Empty).Trim();
            if (string.IsNullOrEmpty(phone) || string.IsNullOrEmpty(key))
            {
                return(Result.Empty);
            }

            var cacheCheck = BuildCacheKey("check" + phone);

            int.TryParse(CheckCache.Get <string>(cacheCheck), out var counter);
            if (++counter > AttemptCount)
            {
                return(Result.TooMuch);
            }
            CheckCache.Insert(cacheCheck, counter.ToString(CultureInfo.InvariantCulture), DateTime.UtcNow.Add(StoreInterval));

            lock (KeyLocker)
            {
                var cacheKey  = BuildCacheKey(phone);
                var phoneKeys = KeyCache.Get <Dictionary <string, DateTime> >(cacheKey);
                if (phoneKeys == null)
                {
                    return(Result.Timeout);
                }

                if (!phoneKeys.ContainsKey(key))
                {
                    return(Result.Invalide);
                }

                var createDate = phoneKeys[key];
                SmsKeyStorageCache.RemoveFromCache(cacheKey);
                if (createDate.Add(StoreInterval) < DateTime.UtcNow)
                {
                    return(Result.Timeout);
                }

                CheckCache.Insert(cacheCheck, (--counter).ToString(CultureInfo.InvariantCulture), DateTime.UtcNow.Add(StoreInterval));
                return(Result.Ok);
            }
        }
Exemple #3
0
        public bool GenerateKey(string phone, out string key)
        {
            if (string.IsNullOrEmpty(phone))
            {
                throw new ArgumentNullException("phone");
            }

            lock (KeyLocker)
            {
                var cacheKey  = BuildCacheKey(phone);
                var phoneKeys = KeyCache.Get <Dictionary <string, DateTime> >(cacheKey) ?? new Dictionary <string, DateTime>();
                if (phoneKeys.Count > AttemptCount)
                {
                    key = null;
                    return(false);
                }

                key            = new Random().Next((int)Math.Pow(10, KeyLength - 1), (int)Math.Pow(10, KeyLength)).ToString(CultureInfo.InvariantCulture);
                phoneKeys[key] = DateTime.UtcNow;

                KeyCache.Insert(cacheKey, phoneKeys, DateTime.UtcNow.Add(StoreInterval));
                return(true);
            }
        }