How to validate ReCAPTCHA.
November 3, 2009 by Nauman · 7 Comments
There are some cases when a user want to validate a CAPTCHA and perform some activity on its results. ReCAPTCHA gives an .NET API to perform some operations on its CAPTCHA component. Following is the code to validate a CAPTCHA on server side.
ASPX Markup:
<!– Start ReCAPTCHA –>
<span class="orangetext">Captcha Verification:</span>
<recaptcha:RecaptchaControl
ID="recaptcha"
runat="server"
PublicKey = Public Key
PrivateKey=Private Key
Theme="blackglass" />
<!– End ReCAPTCHA –>
Code:
String challenge = Request["recaptcha_challenge_field"];
String reponseField = Request["recaptcha_response_field"];
String privateKey = "6LeptwEAAAAAAAQkSaGs-eb5ak6UaJrFUBLcRlKO";Recaptcha.RecaptchaValidator abc = new Recaptcha.RecaptchaValidator();
abc.Challenge = challenge;
abc.PrivateKey = privateKey;
abc.RemoteIP = Request.ServerVariables["REMOTE_ADDR"];
abc.Response = reponseField;Recaptcha.RecaptchaResponse resp = abc.Validate();
if (!resp.IsValid)
{
Response.Write("<pPlease input correct verification code!</p>");
return;
}
Where do I put the code to validate the re-captcha? I already have a form that submits a new account but I am not sure where or how to add the second part labeled “code:”
Add that validation code, in code behind of the page in submission logic of webpage.
Just copy/paste that code into my form? Can we take this offline maybe? I can email you my code if you are willing to help.
Thanks
hmm.. can you elaborate what type of exception/issue you are getting…
have you included Recaptcha reference in your code ?
Yes.
Basically I have a registration form that when I include the first part of the code and hit ‘join’ I get an error, it seems as though the reCAPTCHA is interferring with another module which emails me when a new account is created.
This is the message I get:
User Agent:
Path: as HTTP
Referrer:
Message: Error occurred while running background task PokeSite
System.Exception: No CSContext exists in the Current Application. AutoCreate fails since HttpContext.Current is not accessible.
at CommunityServer.Components.CSContext.get_Current()
at JeffDavis.CS.Modules.PokeSite.Execute(XmlNode prefNode)
at Telligent.Tasks.Task.ExecuteTask()
I turned that off and I still get an error message. It appears that the page wont allow you to click ‘join’ without entering the recaptcha, and when you do it redirects me to a default error page (basic communityserver message) but nothing showing in the logs as to what the problem is.
I am stuck.
This is what I have far as reCAPTCHA in my form (wont post the entire thing, way too long):
That’s it.
I copy/pasted my code into the last post but its not displaying. Either way you can grab see a copy of the code here:
http://mysite.verizon.net/patten23/createuser.txt