Implementing reCaptcha in ASP.NET

2019-08-09 03:02发布

问题:

I am trying to insert a captcha in my ASP.NET code. Basically, in the lbt_proceed_click() method, I want the browser to proceed to the next page using Response.Redirect("foo") only if the captcha entered is correct.

I searched, but could not find a solution, especially since I am not using a form to send data, but writing to a database directly, and then moving to the next page using Response.Redirect().

回答1:

  1. go to the reCAPTCHA site and register for a unique key
  2. Download reCAPTCHA .NET Library
  3. Create and Save your Public and Private keys safely
  4. In the website we add a reference to library/bin/Release/Recaptcha.dll
  5. after the @Page directive, insert the following code:

    <%@ Register TagPrefix="recaptcha" Namespace="Recaptcha" Assembly="Recaptcha"%>
    
  6. add control in asp.net tag:

            <recaptcha:RecaptchaControl
    
            ID="recaptcha"
    
            runat="server"
    
            PublicKey="Your very own public key here"
    
            PrivateKey="Your very own privat key here"
    
       />
    
  7. add button and label to your form

  8. add the following button click method(btnSubmit_Click) in code behind file :

    if (Page.IsValid)
    {   
        lblResult.Text = "You Got It!"; // Or Use Response.redirect("foo");
    }
         else
    {
        lblResult.Text = "Incorrect";
    }
    
  9. Test your Page!