Created
October 31, 2019 00:27
-
-
Save hancheester/4b863fd0dd3d4a4d49b3d469a1677c8c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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