Encapsulates a response from reCAPTCHA web service.
コード例 #1
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.Response == null)
                        {
                            this.recaptchaResponse = RecaptchaResponse.InvalidResponse;
                        }
                        else
                        {
                            this.recaptchaResponse = validator.Validate();
                        }
                    }
                }
            }
            else
            {
                this.recaptchaResponse = RecaptchaResponse.Valid;
            }
        }
コード例 #2
0
ファイル: RecaptchaControl.cs プロジェクト: rheldt/mojoportal
        public void Validate()
        {
            if (Page.IsPostBack && Visible && Enabled && !SkipRecaptcha)
            {
                if (recaptchaResponse == null)
                {
                    if (Visible && Enabled)
                    {
                        RecaptchaValidator validator = new RecaptchaValidator
                        {
                            VerifyUrl  = VerifyUrl,
                            PrivateKey = PrivateKey,
                            RemoteIP   = Page.Request.UserHostAddress,
                            Response   = Context.Request.Form[ResponseField],
                            Proxy      = Proxy
                        };

                        if (validator.Response == null)
                        {
                            recaptchaResponse = RecaptchaResponse.InvalidResponse;
                        }
                        else
                        {
                            recaptchaResponse = validator.Validate().GetAwaiter().GetResult();
                        }
                    }
                }
            }
            else
            {
                recaptchaResponse = RecaptchaResponse.Valid;
            }
        }
コード例 #3
0
        /// <summary>
        /// Perform validation of reCAPTCHA.
        /// </summary>
        public void Validate()
        {
            if (Page.IsPostBack && Visible && Enabled && !SkipRecaptcha)
            {
                if (recaptchaResponse == null)
                {
                    if (Visible && Enabled)
                    {
                        RecaptchaValidator validator = new RecaptchaValidator
                        {
                            VerifyUrl  = VerifyUrl,
                            PrivateKey = PrivateKey,
                            RemoteIP   = Page.Request.UserHostAddress,
                            Response   = Context.Request.Form[ResponseField],
                            Proxy      = Proxy
                        };

                        //validator.Challenge = Context.Request.Form[RECAPTCHA_CHALLENGE_FIELD];
                        if (validator.Response == null)
                        {
                            recaptchaResponse = RecaptchaResponse.InvalidResponse;
                        }
                        else
                        {
                            recaptchaResponse = validator.Validate();
                        }
                    }
                }
            }
            else
            {
                recaptchaResponse = RecaptchaResponse.Valid;
            }
        }
コード例 #4
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;
            }
        }
コード例 #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);
        }
コード例 #6
0
        internal RecaptchaResponse(bool isValid, string[] errorMessages)
        {
            RecaptchaResponse templateResponse = null;

            if (IsValid)
            {
                templateResponse = Valid;
            }
            else
            {
                var errors = new List <string>(errorMessages);

                if (errors.Contains("invalid-input-response"))
                {
                    templateResponse = InvalidSolution;
                }

                if (errors.Contains("missing-input-response"))
                {
                    templateResponse = InvalidResponse;
                }

                if (errorMessages == null)
                {
                    throw new ArgumentNullException("errorMessages");
                }
            }

            if (templateResponse != null)
            {
                IsValid       = templateResponse.IsValid;
                ErrorMessages = templateResponse.ErrorMessages;
            }
            else
            {
                IsValid       = isValid;
                ErrorMessages = errorMessages;
            }
        }
コード例 #7
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];

                        //recaptcha will add a textarea element with id='g-recaptcha-response' for every recaptcha instance
                        //this means if we have more than one on a page, the validation doesn't always work
                        // so, by looping through all form keys with the id 'g-recaptcha-response' and only using the ones
                        // with a value, we're sure to validate properly.
                        // we will then pass the final value to recaptcha to make sure it's valid
                        string response = string.Empty;
                        foreach (string key in Context.Request.Form.AllKeys)
                        {
                            if (key.StartsWith(RECAPTCHA_RESPONSE_FIELD))
                            {
                                if (!String.IsNullOrWhiteSpace(Context.Request.Form[key]))
                                {
                                    response = Context.Request.Form[key];
                                }
                            }
                        }

                        validator.Response = response;
                        validator.Proxy = this.proxy;

                        if (validator.Response == null)
                        {
                            this.recaptchaResponse = RecaptchaResponse.InvalidResponse;
                        }
                        else
                        {
                            this.recaptchaResponse = validator.Validate();
                        }
                    }
                }
            }
            else
            {
                this.recaptchaResponse = RecaptchaResponse.Valid;
            }
        }
コード例 #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;
            }
        }