using System; using System.Configuration; using System.Web; using Newtonsoft.Json; /// /// recaptcha 的摘要描述 /// namespace MyWeb { public class Recaptcha : function { public string Sitekey { get; set; } public string Secret { get; set; } public Recaptcha() { Sitekey = ConfigurationManager.AppSettings["recaptchaSitekey"].ToString(); Secret = ConfigurationManager.AppSettings["recaptchaSecret"].ToString(); } public bool Used { get { return !isStrNull(Sitekey) && !isStrNull(Secret) ? true : false; } } public string HeaderCode(bool inUpdatePanel = false) { //若要指定語系,在api.js加上 hl=語系代碼 string js = ""; if (inUpdatePanel) js += ""; return js; } public string Embed() { return "
"; } public bool Verification(System.Web.UI.UpdatePanel upl = null) { if (upl != null) AjaxRecaptcha(upl); if (!isStrNull(HttpContext.Current.Request["g-recaptcha-response"])) { string postData = "response=" + HttpContext.Current.Request["g-recaptcha-response"].ToString() + "&secret=" + Secret; //要post的資料 WreqInfo info = WRequest("https://www.google.com/recaptcha/api/siteverify", Method.POST, postData); if (isStrNull(info.log)) { Json result = JsonConvert.DeserializeObject(info.data); if (result.success) return true; } else { //Response.Write(info.log); } } return false; } public void AjaxRecaptcha(System.Web.UI.UpdatePanel upl) { System.Web.UI.ScriptManager.RegisterStartupScript(upl, upl.GetType(), "loadCaptcha", "grecaptcha.render('recaptcha', {'sitekey': '" + Sitekey + "' });", true); } public class Json { public bool success { get; set; } public DateTime challenge_ts { get; set; } public string hostname { get; set; } } } }