Ejemplo n.º 1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="RecaptchaResponse"/> class.
        /// </summary>
        /// <param name="isValid">Value indicates whether submitted reCAPTCHA is valid.</param>
        /// <param name="errorCode">Error code returned from reCAPTCHA web service.</param>
        internal RecaptchaResponse(bool isValid, string errorMessage)
        {
            RecaptchaResponse templateResponse = null;

            if (IsValid)
            {
                templateResponse = RecaptchaResponse.Valid;
            }
            else
            {
                switch (errorMessage)
                {
                case "incorrect-captcha-sol":
                    templateResponse = RecaptchaResponse.InvalidSolution;
                    break;

                case null:
                    throw new ArgumentNullException("errorMessage");
                }
            }

            if (templateResponse != null)
            {
                this.isValid      = templateResponse.IsValid;
                this.errorMessage = templateResponse.ErrorMessage;
            }
            else
            {
                this.isValid      = isValid;
                this.errorMessage = errorMessage;
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Perform validation of reCAPTCHA.
        /// </summary>
        public void Validate()
        {
            if (Page.IsPostBack && Visible && Enabled && !this.skipRecaptcha)
            {
                if (this.recaptchaResponse == null)
                {
                    if (Visible && Enabled)
                    {
                        RecaptchaValidator validator = new RecaptchaValidator();
                        validator.PrivateKey = this.PrivateKey;
                        validator.RemoteIP   = Page.Request.UserHostAddress;
                        validator.Challenge  = Context.Request.Form[RECAPTCHA_CHALLENGE_FIELD];
                        validator.Response   = Context.Request.Form[RECAPTCHA_RESPONSE_FIELD];
                        validator.Proxy      = this.proxy;

                        if (validator.Challenge == null)
                        {
                            this.recaptchaResponse = RecaptchaResponse.InvalidChallenge;
                        }
                        else if (validator.Response == null)
                        {
                            this.recaptchaResponse = RecaptchaResponse.InvalidResponse;
                        }
                        else
                        {
                            this.recaptchaResponse = validator.Validate();
                        }
                    }
                }
            }
            else
            {
                this.recaptchaResponse = RecaptchaResponse.Valid;
            }
        }
Ejemplo n.º 3
0
            public override void OnActionExecuting(ActionExecutingContext filterContext)
            {
                if (SkipRecaptcha)
                {
                    filterContext.ActionParameters["captchaValid"] = true;
                }
                else
                {
                    RecaptchaValidator validator = new Recaptcha.RecaptchaValidator();
                    validator.PrivateKey = RecaptchaControlMvc.PrivateKey;
                    validator.RemoteIP   = filterContext.HttpContext.Request.UserHostAddress;
                    validator.Challenge  = filterContext.HttpContext.Request.Form[CHALLENGE_FIELD_KEY];
                    validator.Response   = filterContext.HttpContext.Request.Form[RESPONSE_FIELD_KEY];
                    validator.Proxy      = proxy;

                    if (string.IsNullOrEmpty(validator.Challenge))
                    {
                        this.recaptchaResponse = RecaptchaResponse.InvalidChallenge;
                    }
                    else if (string.IsNullOrEmpty(validator.Response))
                    {
                        this.recaptchaResponse = RecaptchaResponse.InvalidResponse;
                    }
                    else
                    {
                        this.recaptchaResponse = validator.Validate();
                    }

                    // this will push the result values into a parameter in our Action
                    filterContext.ActionParameters["captchaValid"]        = this.recaptchaResponse.IsValid;
                    filterContext.ActionParameters["captchaErrorMessage"] = this.recaptchaResponse.ErrorMessage;
                }

                base.OnActionExecuting(filterContext);
            }
Ejemplo n.º 4
0
        /// <summary>
        /// Perform validation of reCAPTCHA.
        /// </summary>
        public void Validate()
        {
            if (Page.IsPostBack && Visible && Enabled && (GetHiddenFieldControl().Value != "offline") && !_skipRecaptcha)
            {
                if (_recaptchaResponse == null)
                {
                    if (Visible && Enabled)
                    {
                        RecaptchaValidator validator = new RecaptchaValidator
                        {
                            SecretKey = SecretKey,
                            Response  = Context.Request.Form[RecaptchaResponseField]
                        };

                        _recaptchaResponse = validator.Response == null
                            ? RecaptchaResponse.InvalidResponse
                            : validator.Validate();
                    }
                }
            }
            else
            {
                _recaptchaResponse = RecaptchaResponse.Valid;
            }
        }
Ejemplo n.º 5
0
        public override bool Equals(object obj)
        {
            RecaptchaResponse other = (RecaptchaResponse)obj;

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

            return(other.IsValid == this.isValid && other.ErrorMessage == this.errorMessage);
        }
Ejemplo n.º 6
0
            public override void OnActionExecuting(ActionExecutingContext filterContext)
            {
                if (SkipRecaptcha)
                {
                    filterContext.ActionParameters["captchaValid"] = true;
                }
                else
                {
                    RecaptchaValidator validator = new Recaptcha.RecaptchaValidator();
                    validator.PrivateKey = RecaptchaControlMvc.PrivateKey;
                    validator.RemoteIP = filterContext.HttpContext.Request.UserHostAddress;
                    validator.Challenge = filterContext.HttpContext.Request.Form[CHALLENGE_FIELD_KEY];
                    validator.Response = filterContext.HttpContext.Request.Form[RESPONSE_FIELD_KEY];
                    validator.Proxy = proxy;

                    if (string.IsNullOrEmpty(validator.Challenge))
                    {
                        this.recaptchaResponse = RecaptchaResponse.InvalidChallenge;
                    }
                    else if (string.IsNullOrEmpty(validator.Response))
                    {
                        this.recaptchaResponse = RecaptchaResponse.InvalidResponse;
                    }
                    else
                    {
                        this.recaptchaResponse = validator.Validate();
                    }

                    // this will push the result values into a parameter in our Action
                    filterContext.ActionParameters["captchaValid"] = this.recaptchaResponse.IsValid;
                    filterContext.ActionParameters["captchaErrorMessage"] = this.recaptchaResponse.ErrorMessage;
                }

                base.OnActionExecuting(filterContext);
            }
Ejemplo n.º 7
0
        /// <summary>
        /// The validate.
        /// </summary>
        public void Validate()
        {
            if (this.skipRecaptcha)
            {
                this.recaptchaResponse = RecaptchaResponse.Valid;
            }
            else
            {
                var validator = new RecaptchaValidator
                    {
                       PrivateKey = this.privateKey, RemoteIP = Utils.GetClientIP()
                    };
                if (String.IsNullOrEmpty(this.RecaptchaChallengeValue) &&
                    String.IsNullOrEmpty(this.RecaptchaResponseValue))
                {
                    validator.Challenge = this.Context.Request.Form[RecaptchaChallengeField];
                    validator.Response = this.Context.Request.Form[RecaptchaResponseField];
                }
                else
                {
                    validator.Challenge = this.RecaptchaChallengeValue;
                    validator.Response = this.RecaptchaResponseValue;
                }

                this.recaptchaResponse = validator.Validate();
            }
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Perform validation of reCAPTCHA.
        /// </summary>
        public void Validate()
        {
            if (Page.IsPostBack && Visible && Enabled && !this.skipRecaptcha)
            {
                if (this.recaptchaResponse == null)
                {
                    if (Visible && Enabled)
                    {
                        RecaptchaValidator validator = new RecaptchaValidator();
                        validator.PrivateKey = this.PrivateKey;
                        validator.RemoteIP = Page.Request.UserHostAddress;
                        validator.Challenge = Context.Request.Form[RECAPTCHA_CHALLENGE_FIELD];
                        validator.Response = Context.Request.Form[RECAPTCHA_RESPONSE_FIELD];
                        validator.Proxy = this.proxy;

                        if (validator.Challenge == null)
                        {
                            this.recaptchaResponse = RecaptchaResponse.InvalidChallenge;
                        }
                        else if (validator.Response == null)
                        {
                            this.recaptchaResponse = RecaptchaResponse.InvalidResponse;
                        }
                        else
                        {
                            this.recaptchaResponse = validator.Validate();
                        }
                    }
                }
            }
            else
            {
                this.recaptchaResponse = RecaptchaResponse.Valid;
            }
        }
Ejemplo n.º 9
0
        public void Validate()
        {
            if (skipRecaptcha) {
                recaptchaResponse = RecaptchaResponse.Valid;
                isValid = true;
            } else {
                RecaptchaValidator validator = new RecaptchaValidator();
                validator.PrivateKey = PrivateKey;
                validator.RemoteIP = Page.Request.UserHostAddress;
                validator.Challenge = Context.Request.Form[RECAPTCHA_CHALLENGE_FIELD];
                validator.Response = Context.Request.Form[RECAPTCHA_RESPONSE_FIELD];

                recaptchaResponse = validator.Validate();
                isValid = recaptchaResponse.IsValid;
            }
        }
Ejemplo n.º 10
0
        public void Validate()
        {
            if (skipRecaptcha)
            {
                recaptchaResponse = RecaptchaResponse.Valid;
            }
            else
            {
                RecaptchaValidator validator = new RecaptchaValidator();
                validator.PrivateKey = privateKey;
                validator.RemoteIP = Page.Request.UserHostAddress;
                if (String.IsNullOrEmpty(RecaptchaChallengeValue) && String.IsNullOrEmpty(RecaptchaResponseValue))
                {
                    validator.Challenge = Context.Request.Form[RECAPTCHA_CHALLENGE_FIELD];
                    validator.Response = Context.Request.Form[RECAPTCHA_RESPONSE_FIELD];
                }
                else
                {
                    validator.Challenge = RecaptchaChallengeValue;
                    validator.Response = RecaptchaResponseValue;
                }

                recaptchaResponse = validator.Validate();
            }
        }