Created
March 19, 2015 15:00
-
-
Save dampee/c7d5c65c1ea620a0e989 to your computer and use it in GitHub Desktop.
recaptcha v2
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
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