recaptcha
reCaptcha v4.0.30319
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace acushine.Utilities
{
public class ReCaptchaClass
{
public static string Validate(string EncodedResponse)
{
var client = new System.Net.WebClient();
string SecretKey = "key";
var GoogleReply = client.DownloadString(string.Format("https://www.google.com/recaptcha/api/siteverify?secret={0}&response={1}", SecretKey, EncodedResponse));
var captchaResponse = Newtonsoft.Json.JsonConvert.DeserializeObject<ReCaptchaClass>(GoogleReply);
return captchaResponse.Success.ToLower();
}
[JsonProperty("success")]
public string Success
{
get { return m_Success; }
set { m_Success = value; }
}
private string m_Success;
[JsonProperty("error-codes")]
public List<string> ErrorCodes
{
get { return m_ErrorCodes; }
set { m_ErrorCodes = value; }
}
private List<string> m_ErrorCodes;
}
}
//public class ReCaptchaClass
//{
// private static ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// private static string SecretKey = System.Configuration.ConfigurationManager.AppSettings["Google.ReCaptcha.Secret"];
// [JsonProperty("success")]
// public bool Success { get; set; }
// [JsonProperty("error-codes")]
// public List<string> ErrorCodes { get; set; }
// public static async Task<bool> Validate(HttpRequestBase Request)
// {
// string encodedResponse = Request.Form["g-Recaptcha-Response"];
// string remoteIp = Request.UserHostAddress;
// using (var client = new HttpClient())
// {
// var values = new Dictionary<string, string>
// {
// {"secret", SecretKey},
// {"remoteIp", remoteIp},
// {"response", encodedResponse}
// };
// var content = new FormUrlEncodedContent(values);
// var response = await client.PostAsync("https://www.google.com/recaptcha/api/siteverify", content);
// var responseString = await response.Content.ReadAsStringAsync();
// var captchaResponse = Newtonsoft.Json.JsonConvert.DeserializeObject<ReCaptchaClass>(responseString);
// if ((captchaResponse.ErrorCodes?.Count ?? 0) != 0)
// {
// log.Warn("ReCaptcha errors: " + string.Join("\n", captchaResponse.ErrorCodes));
// }
// return captchaResponse.Success;
// }
// }
//}
Comments
Post a Comment