Adding reCAPTCHA v2 to asp.net webpage

-- -- 877 Reputation points
2022-08-10T18:41:41.547+00:00

Hi

I have a simple asp.net webform which has the reCAPTCHA v2 code added to it. Web page code is below.

The reCAPTCHA v2 image appears OK.

How can I now block form submission if user fails reCAPTCHA v2?

Thanks

Regards

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Application.aspx.vb" Inherits="ApplicationForm.Application" %>  
<html>  
   <head>  
      <script>  
      function doSubmit(btnSubmit) {  
        if (typeof (Page_ClientValidate) == 'function' && Page_ClientValidate() == false)   
        {  
          return false;  
        }  
        btnSubmit.disabled = 'disabled';  
        btnSubmit.value = 'Processing. This may take a few moments...';  
        <%= ClientScript.GetPostBackEventReference(btnSubmit, string.Empty) %>;  
      }  
      </script>  
      <script src="https://www.google.com/recaptcha/api.js" async defer></script>  
   </head>  
   <body>  
         <form id="form1" runat="server" class="wpcf7">  
            <asp:TextBox ID="forename" runat="server" ></asp:TextBox>  
            <asp:TextBox ID="dob" runat="server" TextMode="Date"></asp:TextBox>  
            <asp:RequiredFieldValidator  id="abc" runat="server" ControlToValidate="dob" ErrorMessage="DOB missing"></asp:RequiredFieldValidator>  

            <asp:Button ID="btnSubmit" runat="server" Text="Send your details" OnClick="btnSubmit_Click" OnClientClick="doSubmit(this)" />  
  
            <div class="g-recaptcha" data-sitekey="MyreCAPTCHAv2SiteKeyHere"></div>  
         </form>  
   </body>  
</html>  
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,288 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. XuDong Peng-MSFT 10,176 Reputation points Microsoft Vendor
    2022-08-16T02:13:04.293+00:00

    Hi @-- -- ,

    Based on your requirement, you can try to use its callback function to implement client-side validation and allow form submission based on this result. Simply refer to the following code samples:

    Page code:

    <div id="dvCaptcha">  
    </div>  
    <asp:TextBox ID="txtCaptcha" runat="server" Style="display: none" />  
    <asp:RequiredFieldValidator ID="rfvCaptcha" ErrorMessage="Captcha validation is required." ControlToValidate="txtCaptcha"  
        runat="server" ForeColor="Red" Display="Dynamic" />  
    <br />  
    <asp:Button ID="btnSubmit" Text="Submit" runat="server" />  
    

    VB.NET code:

    Partial Class Default  
        Inherits System.Web.UI.Page  
        Protected Shared ReCaptcha_Key As String = "<RECaptcha Site Key>"  
        Protected Shared ReCaptcha_Secret As String = "<RECaptcha Secret Key>"  
       
        <WebMethod()> _  
        Public Shared Function VerifyCaptcha(response As String) As String  
            Dim url As String = "https://www.google.com/recaptcha/api/siteverify?secret=" & ReCaptcha_Secret & "&response=" & response  
            Return (New WebClient()).DownloadString(url)  
        End Function  
    End Class  
    

    Script code:
    230225-image.png

    Best regards,
    Xudong Peng


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments