I’m trying to implement reCaptcha version 2 on my umbraco v7 build that is already live.
Now I have the client-side verification working and would like to verify the key that’s provided with google.
It would be ideal if I could do this with javascript but would be grateful for any solution that would work.
Thanks in advance for any help you can provide.
James
User complete validation on page, which generates token. This is done via javascript on page.
This token then need to be validated on Server. If this validation is not done then form post can still be done by by-passing Javascript. This can be achieved by writing a simple http POST script
Simple add this method to your Controller (or SurfaceController in Umbraco)
private async Task<bool> ValidateReCaptchaV2()
{
home = Umbraco.ContentAtRoot().FirstOrDefault();
//Validate ReCaptcha Using HttpClient
if (!string.IsNullOrEmpty(home.Value<string>("RecaptchaSiteKey")))
{
var captchaResponse = Request.Form["recaptchaCode"];
var secretKey = home.Value<string>("ReCaptchaSecretKey");
var apiUrl = "https://www.google.com/recaptcha/api/siteverify?secret={0}&response={1}";
var requestUri = string.Format(apiUrl, secretKey, captchaResponse);
//PROXY Settings implemented through Web.Config
/*
//Setup the Proxy otherwise its failing on IONOS Web Hosting
//WebRequest wrGETURL;
//wrGETURL = WebRequest.Create(TextBox1.Text);
WebProxy ionosProxy = new WebProxy("http://winproxy.server.lan:3128/", true);
//wrGETURL.Proxy = myProxy;
// Create a client handler that uses the proxy
var httpClientHandler = new HttpClientHandler
{
Proxy = ionosProxy,
};
// Disable SSL verification
httpClientHandler.ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator;
var client = new HttpClient(handler: httpClientHandler, disposeHandler: true);
*/
var client = new HttpClient();
using (var response = client.GetAsync(requestUri))
{
var jsonString = await response.Result.Content.ReadAsStringAsync();
if (response.IsCompleted)
{
var result = JObject.Parse(jsonString);
//https://developers.google.com/recaptcha/docs/verify json format
return result.Value<bool>("success");
}
}
}
return true;
}
Request.Form[“recaptchaCode”] is the code you receive when user completes javascript action
ReCaptchaSecretKey - this is what you required to use when calling Google ReCaptcha Api for validation. You can store this in Web.Config file as well.