/// <summary>Performs validation of the reCAPTCHA component.</summary>
        /// <param name="validationContext">The validation context.</param>
        /// <returns>A collection that holds failed-validation information.</returns>
        public override IEnumerable <ValidationResult> Validate(
            ValidationContext validationContext)
        {
            var validationResultList = new List <ValidationResult>();

            validationResultList.AddRange(base.Validate(validationContext));
            if (!this.IsConfigured | VirtualContext.IsInitialized)
            {
                return((IEnumerable <ValidationResult>)validationResultList);
            }
            var recaptchaValidator = new InvisibleRecaptchaValidator
            {
                PrivateKey = PrivateKey,
                RemoteIP   = RequestContext.UserHostAddress,
                Response   = RecaptchaResponse
            };

            var recaptchaResponse = recaptchaValidator.Validate();

            if (recaptchaResponse != null)
            {
                if (!string.IsNullOrEmpty(recaptchaResponse.ErrorMessage))
                {
                    validationResultList.Add(new ValidationResult(recaptchaResponse.ErrorMessage));
                }
                if (Action != null && !CMSString.Equals(Action, recaptchaResponse.Action))
                {
                    validationResultList.Add(new ValidationResult(ResHelper.GetString("recaptcha.error.actioninvalid")));
                }
                if (recaptchaResponse.Score < Score)
                {
                    validationResultList.Add(new ValidationResult(ResHelper.GetString("recaptcha.error.scoreinvalid")));
                }
            }
            else
            {
                validationResultList.Add(new ValidationResult(ResHelper.GetString("recaptcha.error.serverunavailable",
                                                                                  (string)null, true)));
            }

            return((IEnumerable <ValidationResult>)validationResultList);
        }
Esempio n. 2
0
        public override bool IsValid(object value)
        {
            var responseValue = ValidationHelper.GetString(value, string.Empty);

            if (!IsConfigured | VirtualContext.IsInitialized)
            {
                return(false);
            }

            var recaptchaValidator = new InvisibleRecaptchaValidator
            {
                PrivateKey = PrivateKey,
                RemoteIP   = RequestContext.UserHostAddress,
                Response   = responseValue
            };

            var recaptchaResponse = recaptchaValidator.Validate();

            if (recaptchaResponse != null)
            {
                if (!string.IsNullOrEmpty(recaptchaResponse.ErrorMessage))
                {
                    return(false);
                }
                if (Action != null && !CMSString.Equals(Action, recaptchaResponse.Action))
                {
                    return(false);
                }
                if (recaptchaResponse.Score < Score)
                {
                    return(false);
                }
            }
            else
            {
                return(false);
            }

            return(true);
        }