Recaptcha does not work in asp.net

I use Recaptcha to stop spam on my site. Here is the code for Recaptcha

<asp:Panel ID="Panel1" runat="server" style="padding:5px" BackColor="White" BorderColor="#999999" BorderStyle="Solid"> <asp:UpdatePanel ID="UpdatePanel1" runat="server"> <ContentTemplate> <br /> <recaptcha:RecaptchaControl ID="recaptcha" runat="server" PrivateKey="XXXX-HIDDEN-XXXX" PublicKey="XXXX-HIDDEN-XXXX" Theme="Red" /> <br /> <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Submit" /> &nbsp; <asp:Button ID="Button3" runat="server" Text="Cancel" /> &nbsp;<asp:Label ID="lblResult" runat="server" Font-Size="Medium" ForeColor="Red"></asp:Label> </ContentTemplate> </asp:UpdatePanel> </asp:Panel> <asp:ModalPopupExtender ID="UpdatePanel1_ModalPopupExtender" runat="server" Enabled="True" PopupControlID="Panel1" TargetControlID="Button2" BackgroundCssClass="modalBackground" CancelControlID="Button3"> </asp:ModalPopupExtender> 

Button1_Click Code:

 protected void Button1_Click(object sender, EventArgs e) { Page.Validate(); if (Page.IsValid) { lblResult.Text = "All Good"; } else { lblResult.Text = "The words you entered are incorrect"; } } 

The purpose of this code is that whenever a user enters something into Recaptcha, if the words are correct, then he will show "All is good", and if not, "The words you entered are incorrect."

But the problem is that when Button1 clicked, the Button1 event is not executed. But when recaptcha does not load (i.e., when the Internet is not connected), Button1_Click() executes and only executes code that is outside the if(Page.isValid) condition if(Page.isValid) . For example, if I modify the click event as follows:

 protected void Button1_Click(object sender, EventArgs e) { lblResult.Text="Not in if condition"; Page.Validate(); if (Page.IsValid) { lblResult.Text = "All Good"; } else { lblResult.Text = "The words you entered are incorrect"; } } 

Then it successfully sets lblResult.Text to "Not in if condition" . But when Recaptcha is loaded, it will not execute even those lines that are outside the If condition.

What I tried to solve:

  • Remote Update Panel: same issue
  • Tested in real domain: same issue
  • Debugging and checking for any exceptions: The absence of an exception is displayed in the Exit> Debug window

Please tell me some other solution to this problem and the reason for its occurrence.

+4
source share
2 answers

I had the same problem, do not use the control, use api directly; i.e:.

Markup:

 <div> <asp:Literal ID="litResult" runat="server" Mode="Encode" /> <script type="text/javascript" src="http://www.google.com/recaptcha/api/challenge?k=<%= RecaptchaPublicKey %>"> </script> <asp:Button ID="btnSubmit" OnClick="btnSubmit_Click" Text="Submit" runat="server" /> </div> 

Code behind:

 private const string RECAPTCHA_CHALLENGE_FIELD = "recaptcha_challenge_field"; private const string RECAPTCHA_RESPONSE_FIELD = "recaptcha_response_field"; protected string RecaptchaPublicKey { get { return ConfigurationManager.AppSettings["RecaptchaPublicKey"]; } } protected void btnSubmit_Click(object sender, EventArgs e) { var validator = new Recaptcha.RecaptchaValidator { PrivateKey = ConfigurationManager.AppSettings["RecaptchaPrivateKey"], RemoteIP = Request.UserHostAddress, Challenge = Context.Request.Form[RECAPTCHA_CHALLENGE_FIELD], Response = Context.Request.Form[RECAPTCHA_RESPONSE_FIELD] }; if (validator.Validate().IsValid) { litResult.Text = "All Good"; } else { litResult.Text = "The words you entered are incorrect"; } } 
+2
source

It didn't work for me - maybe because I had a ValidationGroup set, maybe not. I fixed it by forcing it to check and then check if this is really on the reverse side:

 recaptcha.Validate(); if (recaptcha.IsValid) { /* ...do stuff... */ } 
0
source

Source: https://habr.com/ru/post/1484609/


All Articles