如何重设隐形Recaptcha

时间:2018-06-25 21:39:47

标签: c# asp.net webforms invisible-recaptcha

我有这个小表格,并且在asp.net Webforms中实现了一个不可见的Recaptcha。仅当提交有效时才执行此重新捕获。尽管我设法验证了响应。

我需要为每个提交重置不可见的Recaptcha,但是我似乎无法正常工作。你能帮我吗?

这是我的服务器端代码:

        public string CaptchaSiteKey
    {
        get
        {
            return System.Configuration.ConfigurationManager.AppSettings["CaptchaSiteKey"];
        }
    }

    public string CaptchaSecret
    {
        get
        {
            return System.Configuration.ConfigurationManager.AppSettings["CaptchaSecret"];
        }
    }


    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void b1_Click(object sender, EventArgs e)
    {
        if (!Validates(CaptchaSecret, Request.Form["g-recaptcha-response"]))
        {

        }
        else if (IsValid)
        {
            SendToDatabase();
        }

    }

    public static void SendToDatabase()
    {


    }

    public bool Validates(string privateKey, string reCaptchaResponse)
    {
        string Response = Request["g-recaptcha-response"];//Getting Response String Append to Post Method
        bool Valid = false;
        //Request to Google Server
        HttpWebRequest req = (HttpWebRequest)WebRequest.Create
        (string.Format("https://www.google.com/recaptcha/api/siteverify?secret={0}&response={1}", privateKey, reCaptchaResponse));
        try
        {
            //Google recaptcha Response
            using (WebResponse wResponse = req.GetResponse())
            {

                using (StreamReader readStream = new StreamReader(wResponse.GetResponseStream()))
                {
                    string jsonResponse = readStream.ReadToEnd();
                    if (jsonResponse.Contains("success\": true"))
                    {
                        Valid = true;
                    }


                }
            }

            return Valid;
        }
        catch (WebException ex)
        {
            throw ex;
        }
    }

这是我的html代码和javascript代码

 <pre><html xmlns="http://www.w3.org/1999/xhtml">
 <head runat="server">
   <title></title>
       <script src='https://www.google.com/recaptcha/api.js' async defer>
          </script>
     <script>
    var btnLoginEvent = null;

    function DoLogin() {
        var btn = document.getElementById("<%=b1.ClientID%>");
        btn.onclick = btnLoginEvent;
        btn.click();
    }

    function DoValidate(event) {
        event.preventDefault();
        if (Page_ClientValidate()) {
            grecaptcha.reset();
            grecaptcha.execute();
        }
    }

    function DoLoad() {
        var btn = document.getElementById("<%=b1.ClientID%>");
        btnLoginEvent = btn.onclick;
        btn.onclick = DoValidate;
    }
  </script>
  </head>
   <body>
<form id="form1" runat="server">
    <asp:TextBox ID="txt_name" runat="server" />
    <asp:RequiredFieldValidator
        ControlToValidate="txt_name"
        ErrorMessage="Name"
        Text="please sput something in here"
        runat="server" />
    <div class="g-recaptcha"
        data-sitekey="<%=CaptchaSiteKey%>"
        data-callback="DoLogin"
        data-size="invisible">
    </div>
    <asp:Button ID="b1" Text="Submit" runat="server" CausesValidation="true" OnClick="b1_Click" />
    <asp:ValidationSummary
        HeaderText="You must enter a value in the following fields:"
        DisplayMode="BulletList"
        EnableClientScript="true"
        runat="server" />
</form>
   <script>
    DoLoad();
    </script>
 </body>
</html>

我还将发布放置在Webconfig中的密钥,因为以后可以随时对其进行更改:

  <appSettings>

<add key="CaptchaSiteKey"   value="6LciPmAUAAAAABBbY_Q78Jb_ANU9DJDzwmZ2t9Vj"/>
<add key="CaptchaSecret" value="6LciPmAUAAAAAEz0x42LC-D3V95Hju8MWW_Nsg3l"/>

 </appSettings>

注意:忽略html代码之前的“ pre”,这是我可以放置所有html和javascript代码的一种方式。

0 个答案:

没有答案