Пример #1
0
        /// <inheritdoc cref="ICaptcha.Create" />
        public void Create(HttpContext context)
        {
            var feature = GetCaptchaFeature(context);

            if (feature.HasCaptchaToken)
            {
                store.InvalidateCaptcha(context, feature.CaptchaToken);
            }

            var text    = textGenerator.Generate();
            var blob    = new BinaryBlob(256);
            var token   = new CaptchaToken(blob);
            var cookie  = options.Cookie.Build(context);
            var captcha = new GeneratedCaptcha(token, text, DateTime.UtcNow);

            store.SetCaptcha(context, captcha);

            context.Response.Cookies.Append(
                options.Cookie.Name,
                WebEncoders.Base64UrlEncode(token.Value.GetData()),
                cookie
                );

            logger.LogDebug($"New CAPTCHA was generated: {options.Cookie.Name}={blob.DebugString}");
        }
Пример #2
0
        public void InvalidateCaptcha(HttpContext context, CaptchaToken token)
        {
            if (null == context)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (null == token)
            {
                throw new ArgumentNullException(nameof(token));
            }

            cache.Remove(token.Value);
        }
Пример #3
0
        public GeneratedCaptcha GetCaptcha(HttpContext context, CaptchaToken token)
        {
            if (null == context)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (null == token)
            {
                throw new ArgumentNullException(nameof(token));
            }

            if (cache.TryGetValue(token.Value, out var captcha))
            {
                return(captcha);
            }

            return(null);
        }
Пример #4
0
 public GeneratedCaptcha(CaptchaToken token, char[] captcha, DateTime createdAt)
 {
     Token     = token;
     Captcha   = captcha;
     CreatedAt = createdAt;
 }