/// <summary>
        /// Creates an instance of the <see cref="RecaptchaHtmlHelper"/> class.
        /// </summary>
        /// <param name="publicKey">Sets the public key to be part of the recaptcha HTML.</param>
        public RecaptchaHtmlHelper(string publicKey)
        {
            if (String.IsNullOrEmpty(publicKey))
            {
                throw new InvalidOperationException("Public key cannot be null or empty.");
            }

            this.PublicKey = RecaptchaKeyHelper.ParseKey(publicKey);
        }
        /// <summary>
        /// Creates an instance of the <see cref="RecaptchaHtmlHelper"/> class.
        /// </summary>
        /// <param name="publicKey">Sets the public key of the recaptcha HTML.</param>
        /// <param name="theme">Sets the theme of the recaptcha HTML.</param>
        /// <param name="language">Sets the language of the recaptcha HTML.</param>
        /// <param name="tabIndex">Sets the tab index of the recaptcha HTML.</param>
        public RecaptchaHtmlHelper(string publicKey, RecaptchaTheme theme, string language, int tabIndex)
        {
            this.PublicKey = RecaptchaKeyHelper.ParseKey(publicKey);

            if (String.IsNullOrEmpty(this.PublicKey))
            {
                throw new InvalidOperationException("Public key cannot be null or empty.");
            }

            this.Theme    = theme;
            this.Language = language;
            this.TabIndex = tabIndex;
        }
Beispiel #3
0
        /// <summary>
        /// Creates an instance of the <see cref="RecaptchaHtmlHelperBase"/> class.
        /// </summary>
        /// <param name="publicKey">Sets the public key of the recaptcha HTML.</param>
        /// <param name="theme">Sets the theme of the recaptcha HTML.</param>
        /// <param name="language">Sets the language of the recaptcha HTML.</param>
        /// <param name="tabIndex">Sets the tab index of the recaptcha HTML.</param>
        /// <param name="useSsl">Determines whether to use SSL in reCAPTCHA API URLs.</param>
        /// <param name="withCallBack">Determines if we need a callback for our reCaptch</param>
        public RecaptchaHtmlHelperBase(string publicKey, RecaptchaTheme theme, string language, int tabIndex, SslBehavior useSsl, bool withCallBack)
        {
            this.PublicKey = RecaptchaKeyHelper.ParseKey(publicKey);

            if (String.IsNullOrEmpty(this.PublicKey))
            {
                throw new InvalidOperationException("Public key cannot be null or empty.");
            }

            this.Theme    = theme;
            this.Language = language;
            this.TabIndex = tabIndex;

            UseSsl       = useSsl;
            WithCallback = withCallBack;
        }
Beispiel #4
0
        /// <summary>
        /// Verifies whether the user's response to the recaptcha request is correct.
        /// </summary>
        /// <returns>Returns the result as a value of the <see cref="RecaptchaVerificationResult"/> enum.</returns>
        public Task <RecaptchaVerificationResult> VerifyRecaptchaResponseTaskAsync()
        {
            if (string.IsNullOrEmpty(Response))
            {
                return(FromTaskResult <RecaptchaVerificationResult>(RecaptchaVerificationResult.NullOrEmptyCaptchaSolution));
            }

            string privateKey = RecaptchaKeyHelper.ParseKey(PrivateKey);

            if (_isVersion2)
            {
                return(VerifyRecpatcha2ResponseTaskAsync(privateKey));
            }

            if (string.IsNullOrEmpty(_challenge))
            {
                return(FromTaskResult <RecaptchaVerificationResult>(RecaptchaVerificationResult.ChallengeNotProvided));
            }

            Task <RecaptchaVerificationResult> result = Task <RecaptchaVerificationResult> .Factory.StartNew(() =>
            {
                string postData = String.Format("privatekey={0}&remoteip={1}&challenge={2}&response={3}", privateKey, this.UserHostAddress, this._challenge, this.Response);

                byte[] postDataBuffer = System.Text.Encoding.ASCII.GetBytes(postData);

                Uri verifyUri = null;

                if (!UseSsl)
                {
                    verifyUri = new Uri("http://www.google.com/recaptcha/api/verify", UriKind.Absolute);
                }
                else
                {
                    verifyUri = new Uri("https://www.google.com/recaptcha/api/verify", UriKind.Absolute);
                }

                try
                {
                    HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(verifyUri);
                    webRequest.ContentType    = "application/x-www-form-urlencoded";
                    webRequest.ContentLength  = postDataBuffer.Length;
                    webRequest.Method         = "POST";

                    IWebProxy proxy   = WebRequest.GetSystemWebProxy();
                    proxy.Credentials = CredentialCache.DefaultCredentials;

                    webRequest.Proxy = proxy;

                    Stream requestStream = webRequest.GetRequestStream();
                    requestStream.Write(postDataBuffer, 0, postDataBuffer.Length);

                    HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();

                    string[] responseTokens = null;
                    using (StreamReader sr = new StreamReader(webResponse.GetResponseStream()))
                    {
                        responseTokens = sr.ReadToEnd().Split('\n');
                    }

                    return(ToRecaptchaVerificationResult(responseTokens));
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            });

            return(result);
        }
        /// <summary>
        /// Verifies whether the user's response to the recaptcha request is correct.
        /// </summary>
        /// <returns>Returns the result as a value of the <see cref="RecaptchaVerificationResult"/> enum.</returns>
        public Task <RecaptchaVerificationResult> VerifyRecaptchaResponseTaskAsync()
        {
            if (string.IsNullOrEmpty(_Challenge))
            {
                return(FromTaskResult <RecaptchaVerificationResult>(RecaptchaVerificationResult.ChallengeNotProvided));
            }

            if (string.IsNullOrEmpty(Response))
            {
                return(FromTaskResult <RecaptchaVerificationResult>(RecaptchaVerificationResult.NullOrEmptyCaptchaSolution));
            }

            Task <RecaptchaVerificationResult> result = Task <RecaptchaVerificationResult> .Factory.StartNew(() =>
            {
                string privateKey = RecaptchaKeyHelper.ParseKey(PrivateKey);

                string postData = String.Format("privatekey={0}&remoteip={1}&challenge={2}&response={3}", privateKey, this.UserHostAddress, this._Challenge, this.Response);

                byte[] postDataBuffer = System.Text.Encoding.ASCII.GetBytes(postData);

                Uri verifyUri = null;

                if (!UseSsl)
                {
                    verifyUri = new Uri("//www.google.com/recaptcha/api/verify", UriKind.Absolute);
                }
                else
                {
                    verifyUri = new Uri("//www.google.com/recaptcha/api/verify", UriKind.Absolute);
                }

                try
                {
                    HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(verifyUri);
                    webRequest.ContentType    = "application/x-www-form-urlencoded";
                    webRequest.ContentLength  = postDataBuffer.Length;
                    webRequest.Method         = "POST";

                    IWebProxy proxy   = WebRequest.GetSystemWebProxy();
                    proxy.Credentials = CredentialCache.DefaultCredentials;

                    webRequest.Proxy = proxy;

                    Stream requestStream = webRequest.GetRequestStream();
                    requestStream.Write(postDataBuffer, 0, postDataBuffer.Length);

                    HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();

                    string[] responseTokens = null;
                    using (StreamReader sr = new StreamReader(webResponse.GetResponseStream()))
                    {
                        responseTokens = sr.ReadToEnd().Split('\n');
                    }

                    if (responseTokens.Length == 2)
                    {
                        Boolean success = responseTokens[0].Equals("true", StringComparison.CurrentCulture);

                        if (success)
                        {
                            return(RecaptchaVerificationResult.Success);
                        }
                        else
                        {
                            if (responseTokens[1].Equals("incorrect-captcha-sol", StringComparison.CurrentCulture))
                            {
                                return(RecaptchaVerificationResult.IncorrectCaptchaSolution);
                            }
                            else if (responseTokens[1].Equals("invalid-site-private-key", StringComparison.CurrentCulture))
                            {
                                return(RecaptchaVerificationResult.InvalidPrivateKey);
                            }
                            else if (responseTokens[1].Equals("invalid-request-cookie", StringComparison.CurrentCulture))
                            {
                                return(RecaptchaVerificationResult.InvalidCookieParameters);
                            }
                        }
                    }

                    return(RecaptchaVerificationResult.UnknownError);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            });

            return(result);
        }