migrate to new git
This commit is contained in:
81
web/App_Code/recaptcha.cs
Normal file
81
web/App_Code/recaptcha.cs
Normal file
@@ -0,0 +1,81 @@
|
||||
using System;
|
||||
using System.Configuration;
|
||||
using System.Web;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
/// <summary>
|
||||
/// recaptcha 的摘要描述
|
||||
/// </summary>
|
||||
|
||||
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 = "<script src=\"https://www.google.com/recaptcha/api.js" + (inUpdatePanel ? "?onload=onloadCallback&render=explicit" : "") + "\" async defer></script>";
|
||||
if (inUpdatePanel)
|
||||
js += "<script> var onloadCallback = function () {grecaptcha.render('recaptcha', {'sitekey': '" + Sitekey + "' }); };</script>";
|
||||
return js;
|
||||
}
|
||||
|
||||
public string Embed()
|
||||
{
|
||||
return "<div id=\"recaptcha\" class=\"g-recaptcha\" data-sitekey=\"" + Sitekey + "\"></div>";
|
||||
}
|
||||
|
||||
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<Json>(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; }
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user