Hi, I have a complex form that I am adding a recaptcha to, only problem is that the Recaptcha never validates.
I am running on a local build of a.NET (4) website under IIS6. I have tried 'hard-coding' my IP address according to the IP address of our network as it appears to the outside (REMOTE_ADDR returns 127.0.0.1.
Checklist:
- form tag inside table tag : NOPE (1 form tag under the body tag, div based form layout)
- Invalid Private/Public key : NOPE (Triple checked, even checked visually using a breakpoint!)
- Invalid domain match : NOPE (Is set up as a global key on ReCaptcha)
Anyone have any other ideas? I can not stage this solution unfortunataly, I only have local dev to LIVE due to some internal issues (no IIS stage servers atm!). I really need to get a good response just once before I can roll this to the live server.
Here's my code (captcha validating routine):
private static string ValidateCaptcha(string parameters)
{
try
{
WebRequest request = WebRequest.Create("http://www.google.com/recaptcha/api/verify");
request.ContentType = "application/x-www-form-urlencoded";
request.Method = "POST";
//request.ContentLength = parameters.Length;
StreamWriter writer = new StreamWriter(request.GetRequestStream(),Encoding.UTF8);
writer.Write(parameters);
writer.Close();
//Response
HttpWebResponse webResponse = (HttpWebResponse)request.GetResponse();
StreamReader myReader = new StreamReader(webResponse.GetResponseStream());
string response = myReader.ReadToEnd();
myReader.Close();
webResponse.Close();
return response;
}
catch (WebException)
{
//Do Some Exception threatment
return string.Empty;
}
}And here's the parameter builder ( formControl is the form control I;'m using EPiServer and XForms, reCap uis the recaptcha control, which is holding he correct private key. MY.IP.MY.IP is my hard coded IP for testing ) :
private void validateRecaptcha(object source, ServerValidateEventArgs args){
Debug.WriteLine("validateCatcha");
CustomValidator sVal = (CustomValidator)source;
EPiServer.XForms.WebControls.XFormControl formControl = (EPiServer.XForms.WebControls.XFormControl)sVal.Parent;
if (formControl != null)
{
RecaptchaControl reCap = (RecaptchaControl)formControl.FindControl("Recaptcha");
if (reCap != null)
{
StringBuilder builder = new StringBuilder();
builder.AppendFormat("privatekey={0}&", reCap.PrivateKey);
builder.AppendFormat("remoteip={0}&", "MY.IP.MY.IP"); //Request.ServerVariables["REMOTE_ADDR"]);
builder.AppendFormat("challenge={0}&", Request["recaptcha_challenge_field"]);
builder.AppendFormat("response={0}", Request["recaptcha_response_field"]);
Debug.WriteLine(ValidateCaptcha(builder.ToString()));
}
}
}