Click here to Skip to main content
15,881,380 members
Articles / Web Development / ASP.NET

Custom reCaptcha Validation

Rate me:
Please Sign up or sign in to vote.
4.93/5 (22 votes)
15 Nov 2013CPOL2 min read 89.3K   3K   59  
How to manually integrate reCaptcha into your site
using System;
using System.Collections.Generic;
using System.Web;

using System.IO;
using System.Net;

/// <summary>
/// Summary description for reCaptchaValidation
/// </summary>
public class reCaptchaValidation
{
    private string challenge, response, privateKey, ip;
    private IWebProxy proxy;


    public reCaptchaValidation(string clientIP, string privateKey, string challenge, string response) : this(null, clientIP, privateKey, challenge, response) { }

	public reCaptchaValidation(IWebProxy proxy, string clientIP, string privateKey, string challenge, string response)
	{
        this.proxy = proxy;
        this.ip = clientIP;
        this.privateKey = privateKey;
        this.challenge = challenge;
        this.response = response;
	}

    private bool _errored;
    public bool IsErrored
    {
        get
        {
            return _errored;
        }
    }

    private Exception _ex;
    public Exception Exception
    {
        get
        {
            return _ex;
        }
    }

    private string _vr;
    public string ValidationResult
    {
        get
        {
            return _vr;
        }
    }

    public bool Validate()
    {
        try
        {
            string post = "privatekey=" + HttpUtility.UrlEncode(privateKey) + "&remoteip=" + HttpUtility.UrlEncode(ip) + "&challenge=" + HttpUtility.UrlEncode(challenge) + "&response=" + HttpUtility.UrlEncode(response);

            WebRequest wr = HttpWebRequest.Create("http://www.google.com/recaptcha/api/verify");
            wr.Method = "POST";

            if (proxy != null)
                wr.Proxy = proxy;

            wr.ContentLength = post.Length;
            wr.ContentType = "application/x-www-form-urlencoded";
            using (StreamWriter sw = new StreamWriter(wr.GetRequestStream()))
            {
                sw.Write(post);
                sw.Close();
            }

            HttpWebResponse resp = (HttpWebResponse)wr.GetResponse();
            using (StreamReader sr = new StreamReader(resp.GetResponseStream()))
            {
                string valid = sr.ReadLine();
                if (valid != null)
                {
                    if (valid.ToLower().Trim() == "false")
                    {
                        string errorcode = sr.ReadLine();

                        if (errorcode != null)
                        {
                            if (errorcode.ToLower().Trim() != "incorrect-captcha-sol")
                            {
                                _vr = errorcode;
                                _errored = true;
                                return false;
                            }
                        }
                    }

                    return (valid.ToLower().Trim() == "true");
                }
                else _vr = "empty web service response";

                sr.Close();
                return false;
            }
        }
        catch (Exception caught)
        {
            _errored = true;
            _ex = caught;
        }
        return false;
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
United Kingdom United Kingdom
I've been a web application developer since the late 1990s, working for eCommerce, media and telecoms companies across europe and america.

Comments and Discussions