SParc
SParc

Reputation: 1779

Recaptcha not working in asp.net

I am using Recaptcha to stop spamming 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>

The code for Button1_Click:

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 objective of this code is that, whenever user enters something in Recaptcha, if the words are correct then it will show "All Good" and if not "The words you entered are incorrect".

But the problem is that when I click Button1, its Click event is not executed. But when the recaptcha is not loaded (i.e. when internet is not connected) the Button1_Click() executes and executes only code that is outside the if(Page.isValid) condition. For example if I modify the click event like this:

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 is successfully setting the 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 it:

  1. Removed Update Panel: Same Problem
  2. Tested on actual domain: Same Problem
  3. Debugged and checked for any exception: No Exception showed on Output>Debug screen

Please tell me any other solution for this problem and the reason why its happening.

Upvotes: 4

Views: 4557

Answers (2)

Josh M.
Josh M.

Reputation: 27773

Wasn't working for me either - maybe because I had ValidationGroup set, maybe not. I fixed it by forcing it to validate and then checking if it's valid on post-back:

recaptcha.Validate();

if (recaptcha.IsValid) { /* ...do stuff... */ }

Upvotes: 0

Ondrej Svejdar
Ondrej Svejdar

Reputation: 22054

I was having the same problem too, don't 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";
  }
}

Upvotes: 2

Related Questions