Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何重置TCHA_C#_Asp.net_Webforms_Invisible Recaptcha - Fatal编程技术网

C# 如何重置TCHA

C# 如何重置TCHA,c#,asp.net,webforms,invisible-recaptcha,C#,Asp.net,Webforms,Invisible Recaptcha,我有这个小表单,并且在asp.net Webforms中实现了一个不可见的recaptcha。仅当提交有效时,才会执行此recaptcha。尽管我已经设法证实了这个回答 我需要为每次提交重置不可见的recaptcha,但我似乎无法让它工作。你能帮帮我吗 这是我的服务器端代码: public string CaptchaSiteKey { get { return System.Configuration.Config

我有这个小表单,并且在asp.net Webforms中实现了一个不可见的recaptcha。仅当提交有效时,才会执行此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;
        }
    }
 <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>
这是我的html代码和javascript代码


var btnLoginEvent=null;
函数DoLogin(){
var btn=document.getElementById(“”);
btn.onclick=btnLoginEvent;
点击();
}
函数DoValidate(事件){
event.preventDefault();
如果(第\u页ClientValidate()){
grecaptcha.reset();
grecaptcha.execute();
}
}
函数DoLoad(){
var btn=document.getElementById(“”);
btnLoginEvent=btn.onclick;
btn.onclick=DoValidate;
}
DoLoad();
我还将发布我在网络配置中放置的密钥,因为我可以在之后随时更改它们:


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

  <appSettings>

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

 </appSettings>