Skip to content

Instantly share code, notes, and snippets.

@dampee
Created March 19, 2015 15:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dampee/c7d5c65c1ea620a0e989 to your computer and use it in GitHub Desktop.
Save dampee/c7d5c65c1ea620a0e989 to your computer and use it in GitHub Desktop.
recaptcha v2
using System.Collections.Generic;
using System.Web;
using Newtonsoft.Json;
/// <summary>
/// Captcha helper
/// </summary>
/// <remarks>
/// Original http://stackoverflow.com/questions/27764692/validating-recaptcha-2-no-captcha-recaptcha-in-asp-nets-server-side
/// </remarks>
public class ReCaptcha
{
public static ReCaptcha Validate(string encodedResponse)
{
var client = new System.Net.WebClient();
const string privateKey = ".......";
var userIp = HttpContext.Current.Request.UserHostAddress;
var googleReply = client.DownloadString(string.Format("https://www.google.com/recaptcha/api/siteverify?secret={0}&response={1}", privateKey, encodedResponse));
var captchaResponse = Newtonsoft.Json.JsonConvert.DeserializeObject<ReCaptcha>(googleReply);
return captchaResponse;
}
[JsonProperty("success")]
public bool Success { get; set; }
/// <summary>
/// The error code, if one has occured
/// </summary>
/// <returns>
/// possible return values are found here: https://developers.google.com/recaptcha/docs/verify
/// </returns>
[JsonProperty("error-codes")]
public List<string> ErrorCodes { get; set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment