Beispiel #1
0
        public virtual async Task <string> GenerateAsync(string codeCacheKey, TimeSpan codeCacheLifespan,
                                                         VerificationCodeConfiguration configuration)
        {
            var verificationCode = await _verificationCodeGenerator.CreateAsync(configuration);

            await _cache.SetAsync(codeCacheKey, new VerificationCodeCacheItem(verificationCode),
                                  new DistributedCacheEntryOptions { AbsoluteExpirationRelativeToNow = codeCacheLifespan });

            return(verificationCode);
        }
        public virtual Task <string> CreateAsync(VerificationCodeConfiguration configuration)
        {
            if (configuration.Generate != null)
            {
                return(Task.FromResult(configuration.Generate.Invoke()));
            }

            Check.NotNullOrWhiteSpace(configuration.Chars, nameof(configuration.Chars));

            return(Task.FromResult(RandomString(configuration.Chars, configuration.Length)));
        }
Beispiel #3
0
        public virtual async Task <bool> ValidateAsync(string codeCacheKey, string verificationCode,
                                                       VerificationCodeConfiguration configuration)
        {
            var cacheItem = await _cache.GetAsync(codeCacheKey, true);

            if (cacheItem == null)
            {
                return(false);
            }

            await _cache.RemoveAsync(codeCacheKey);

            return(IsInputCodeCorrect(verificationCode, cacheItem.Code, configuration));
        }
Beispiel #4
0
        protected virtual bool IsInputCodeCorrect(string inputCode, string correctCode,
                                                  VerificationCodeConfiguration configuration)
        {
            if (inputCode.Length != correctCode.Length)
            {
                return(false);
            }

            if (configuration.EquivalentCharsMaps.IsNullOrEmpty())
            {
                return(inputCode == correctCode);
            }

            for (var i = 0; i < inputCode.Length; i++)
            {
                if (!IsCharCorrect(inputCode[i], correctCode[i], configuration.EquivalentCharsMaps))
                {
                    return(false);
                }
            }

            return(true);
        }