Add Google reCAPTCHA v2 to ASP.Net Website

The official document to add google reCAPTCHA v2 to the asp.net website is here:  

https://developers.google.com/recaptcha/docs/display

A good article to follow is here:

https://www.c-sharpcorner.com/article/integration-of-google-recaptcha-in-w

After registering the  reCAPTCHA keys from here:

https://www.google.com/recaptcha/admin


Step 1- Add following code to the aspx file to show the reCAPTCHA control:

        <div class="form-item" id="divRecaptcha" runat="server">

                <div class="slickbutton clearfix" style="margin:5px auto;width:100%;text-align:center;">

                    <div data-theme="light" class="g-recaptcha" data-sitekey="sample public key"></div>

                </div>

           </div>

           <script src="https://www.google.com/recaptcha/api.js" async defer></script>


Step 2 - Add  following code to the C# code behand the page:

    public bool IsReCaptchaValid()

    {

        var result = false;

        var captchaResponse = Request.Form["g-recaptcha-response"];

        var secretKey = "Private Key";

        var apiUrl = "https://www.google.com/recaptcha/api/siteverify?secret={0}&response={1}";

        var requestUri = string.Format(apiUrl, secretKey, captchaResponse);

        var request = (HttpWebRequest)WebRequest.Create(requestUri);

        using (WebResponse response = request.GetResponse())

        {

            using (StreamReader stream = new StreamReader(response.GetResponseStream()))

            {

                JObject jResponse = JObject.Parse(stream.ReadToEnd());

                var isSuccess = jResponse.Value<bool>("success");

                result = (isSuccess) ? true : false;

            }

        }

        return result;

    }


Step 2 - Call the ReCaptcha check function in the Submit event code. If recaptcha not valid, show a warning message on the page:

   bool bIsRobot = IsReCaptchaValid();

        if (bIsRobot == false)

        {

            string strErrorMessage = "<ul>";

            strErrorMessage += "<li>Please check reCAPTCHA.</li>";

            strErrorMessage += "</ul>";

            divcontacte.InnerHtml = strErrorMessage;

            divcontacte.Visible = true;

            return;

        }

        else {

            divRecaptcha.Visible = false;

        }

reCAPTCHA on Webpage


Final note: it is possible to use Google reCAPTCHA in countries where Google is not accessible. Just use "www.recaptcha.net"   instead of "www.google.com" as mentioned in  following article:

 https://developers.google.com/recaptcha/docs/faq#:~:

Comments

  1. I am always searching online for articles that can help me. There is obviously a lot to know about this. I think you made some good points in Features also. Keep working, great job ! 2captcha login

    ReplyDelete

Post a Comment

Popular posts from this blog

Newsmag Theme and External Featured Image

Javascript Hosted on Google Drive Not Working Reliably

How to clear MS SQL Server Database log file on GoDaddy