Recaptcha Validations on C# asp.net

170 views
Skip to first unread message

Jose Reinoso rodriguez

unread,
May 3, 2016, 4:08:40 PM5/3/16
to reCAPTCHA
Hi,


I'm trying to implement the validation for the recaptcha but i do not see any guide where i can follow to meet with validation in asp.net C#.


Can someone help me please.

Seth Munroe

unread,
May 5, 2016, 5:32:51 PM5/5/16
to reCAPTCHA
I use the following:
        /// <summary>
        /// This value will be true if the user passes the test or if the test is unable
        /// to run for any reason.
        /// </summary>
        private bool UserIsHuman
        {
            get
            {
                bool isHuman = true; // assume human until proven otherwise
                lblResult.Text = "";

                // issues in communication with the server will be treated as
                // successful validation.
                try
                {
                    WebClient client = new WebClient();

                    NameValueCollection postParms = new NameValueCollection();
                    postParms.Add("secret", "xxx" );
                    postParms.Add("response", Request.Params["g-recaptcha-response"]);

                    byte[] resp = client.UploadValues("https://www.google.com/recaptcha/api/siteverify", postParms);

                    String respString = Encoding.UTF8.GetString(resp);

                    JavaScriptSerializer jss = new JavaScriptSerializer();
                    dynamic recapVal = jss.DeserializeObject(respString);

                    bool recapSuccess = false;

                    recapSuccess = recapVal["success"];

                    if ((!recapSuccess) ||
                        (!HttpContext.Current.Request.Url.Host.Equals(recapVal["hostname"])) )
                    {
                        isHuman = false;
                    }
                }
                catch (Exception)
                {
                    // ignore it.
                }

                return isHuman;
            }
        }

Reply all
Reply to author
Forward
0 new messages