public override ValueTask <ValidateResult> Validate(CaptchaValidateContext context, BaseCaptchaOptions options)
        {
            var model = context.GetResult();

            model.IsValidate = true;
            model.Count++;
            model.Succeed = false;

            if (context.Validate?.Points?.Count != 1)
            {
                return(new ValueTask <ValidateResult>(model));
            }
            var targetPoint = context.Validate.Points[0];
            var vPoint      = context.Source.Points[0];

            var x     = Math.Abs(vPoint.X - targetPoint.X);
            var y     = Math.Abs(vPoint.Y - targetPoint.Y);
            var range = options?.Offset ?? _options.Offset;

            if (x >= 0 && x <= range && y >= 0 && y <= range)
            {
                model.Succeed       = true;
                model.AllowValidate = false;
            }

            return(new ValueTask <ValidateResult>(model));
        }
Beispiel #2
0
        public virtual bool AllowValidate(CaptchaValidateContext context, BaseCaptchaOptions options = null)
        {
            var result = context.GetResult();

            if (!result.AllowValidate)
            {
                return(result.AllowValidate);
            }
            result.AllowValidate = options == null? result.Count < Options?.Validate_Max: result.Count < options.Validate_Max;
            return(result.AllowValidate);
        }
Beispiel #3
0
        public async ValueTask <IActionResult> Validate([FromBody] CaptchaVerifyModel model)
        {
            if (model == null)
            {
                return(Json(ValidateResult.Failed));
            }
            var data = _memoryCache.Get <CaptchaCacheModel>(cacheKey + model.TK);

            if (data == null)
            {
                return(Json(ValidateResult.Failed));
            }
            var context = new CaptchaValidateContext(new ValidateModel(data.Points), model.ToValidateModel(), data.Validate);

            data.Validate = await _captchaManager.Validate(data.Name, context, data.Options);

            return(Json(data.Validate?.ToViewModel()));
        }
        public async ValueTask <ValidateResult> Validate(string captcha, CaptchaValidateContext context, BaseCaptchaOptions options)
        {
            var validator = _validatorFactory.Create(captcha);

            if (validator == null)
            {
                return(ValidateResult.Failed);
            }
            if (!validator.AllowValidate(context, options))
            {
                return(context.GetResult().NotAllow());
            }

            var handlers = _validateHandlerFactory?.Create(captcha);

            if (handlers?.Any() ?? false)
            {
                handlers.Handing(async(handler, context) => await handler.Validating(context, options), context);
                if (!context.GetResult().AllowValidate)
                {
                    return(context.GetResult().NotAllow());
                }
            }

            await validator.Validate(context, options);

            var result = context.GetResult();

            if (result.Token == null && result.Succeed)
            {
                result.Token = _token.Create();
            }
            if (!result.Succeed)
            {
                result.Token = null;
            }
            if (result.AllowValidate)
            {
                result.AllowValidate = validator.AllowValidate(context, options);
            }
            handlers?.Reverse().Handing(async(handler, context) => await handler.Validated(context, options), context);
            return(result);
        }
        public override ValueTask <ValidateResult> Validate(CaptchaValidateContext context, BaseCaptchaOptions options)
        {
            var model = context.GetResult();

            model.IsValidate = true;
            model.Count++;
            model.Succeed = false;
            var sourcePoints = context.Source.Points;
            var points       = context.Validate.Points;

            if (!points?.Any() ?? true || points.Count != sourcePoints?.Count)
            {
                return(new ValueTask <ValidateResult>(model));
            }

            var offset = options?.Offset ?? _options.Offset;

            model.Succeed = true;
            for (int i = 0; i < points.Count; i++)
            {
                var pitem = points[i];
                var oldp  = sourcePoints[i];
                if (pitem.X >= oldp.X - offset && //验证左边范围
                    pitem.X <= oldp.X + oldp.Width + offset &&    //验证 右边范围
                    pitem.Y >= oldp.Y - offset &&     //验证高度上的范围
                    pitem.Y <= oldp.Y + oldp.Height + offset       //验证高度 下的范围
                    )
                {
                    continue;
                }
                model.Succeed = false;
                break;
            }
            model.AllowValidate = !model.Succeed;
            return(new ValueTask <ValidateResult>(model));
        }
        public ValueTask <ValidateResult> Validate <T>(CaptchaValidateContext context, BaseCaptchaOptions options) where T : ICaptcha, new()
        {
            var type = typeof(T).Name;

            return(Validate(type, context, options));
        }
 public override Task Validating(CaptchaValidateContext context, BaseCaptchaOptions options = null)
 {
     return(Task.CompletedTask);
 }
Beispiel #8
0
 public abstract ValueTask <ValidateResult> Validate(CaptchaValidateContext context, BaseCaptchaOptions options = null);
 public virtual Task Validated(CaptchaValidateContext context, BaseCaptchaOptions options = null)
 {
     return(Task.CompletedTask);
 }