Skip to content

Instantly share code, notes, and snippets.

@hancheester
Created October 31, 2019 00:27
Show Gist options
  • Save hancheester/4b863fd0dd3d4a4d49b3d469a1677c8c to your computer and use it in GitHub Desktop.
Save hancheester/4b863fd0dd3d4a4d49b3d469a1677c8c to your computer and use it in GitHub Desktop.
/*
A function to verify Google reCAPTCHA (server side integration)
*/
protected bool VerifyRecaptcha()
{
var values = new Dictionary<string, string>
{
{ "secret", ConfigurationManager.AppSettings["recaptcha:SecretKey"] },
{ "response", HttpContext.Request["g-recaptcha-response"] },
{ "remoteip", HttpContext.Request.UserHostAddress },
};
var content = new FormUrlEncodedContent(values);
var client = new HttpClient();
var response = Task.Run(() => client.PostAsync("https://www.google.com/recaptcha/api/siteverify", content)).Result;
var responseString = Task.Run(() => response.Content.ReadAsStringAsync()).Result;
dynamic recaptchaResult = JsonConvert.DeserializeObject(responseString);
string success = recaptchaResult.success;
return success.ToLower() == "true";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment