STEP 1:
We have to write javascript which will make an ajax call. The javascript code for that is:
var captchaInfo =
{
challengeValue: Recaptcha.get_challenge(),
responseValue: Recaptcha.get_response(),
});
$.ajax
({
type: 'POST',
url: '<URL of the WebMethod>',
data: JSON.stringify(captchaInfo),
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function(msg)
{
alert(msg.d); // Either true o false, true indicates CAPTCHA is validated successfully.
}
});
STEP 2:
We have to the web service which will catch the Ajax call and validate the captcha, the C# code for that is:
[WebMethod]
public static bool ValidateCaptcha(string challengeValue, string responseValue)
{
Recaptcha.RecaptchaValidator captchaValidtor = new Recaptcha.RecaptchaValidator
{
PrivateKey = Convert.ToString(ConfigurationManager.AppSettings["PrivateKey"]), // Get Private key of the CAPTCHA from Web.config file.
RemoteIP = HttpContext.Current.Request.UserHostAddress,
Challenge = challengeValue,
Response = responseValue
};
Recaptcha.RecaptchaResponse recaptchaResponse = captchaValidtor.Validate(); // Send data about captcha validation to reCAPTCHA site.
return recaptchaResponse.IsValid; // Get boolean value about Captcha success / failure.
}