UncleCoder.com

UncleCoder.com

Free programming examples and instructions

Recaptcha in Asp.net

Demo and Instructions for how to implement recaptcha 2 in asp.net

by Athil


Posted on 27 Jul 2018 Category: Asp.Net Views: 4130


Here I am going to show how to implement google recaptcha 2 in asp.net.

DEMO

Steps to implement recaptcha 2 

1. Open URL https://www.google.com/recaptcha/admin/create

2. Login with your Google account.

3 Enter Label, Select recaptcha v2, I am not robot checkbox and enter the domain, you can add 'localhost' if you want to test from localhost along with the domain.

recaptch2 2 first step

4 Accept Terms and conditions and click on the submit button.

recaptcha 2 Accept terms and conditions

 

5. Note down site key and secret key.

Recaptcha 2 site key and secret key

6. Client side

   Link javascript

   

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

 

Add the div for Recaptcha and give your site key

<div id="recaptcha" class="g-recaptcha" data-sitekey="6Lf6a***********************************"></div>

Server side

Add Namespace

using Newtonsoft.Json.Linq;

Validating recaptcha and give your secret key 

 protected void Button1_Click(object sender, EventArgs e)
        {
            string EncodedResponse = Request.Form["g-Recaptcha-Response"];
            
            string recaptchRespone = recaptchaPost(EncodedResponse);

            dynamic json = JObject.Parse(recaptchRespone);
          //  Response.Write(recaptchRespone);

            if (json.success == true)
            {
                Response.Write("Posted successfully ");

            }
            else {
                Response.Write("Please check the captcha!");

            }
        }
        public string recaptchaPost(string EncodedResponse)
        {

            using (WebClient client = new WebClient())
            {

                byte[] response =
                client.UploadValues("https://www.google.com/recaptcha/api/siteverify", new NameValueCollection()
                   {
                       { "secret", "6Lf6ahMU****************************" },
                       { "response", EncodedResponse }

                   });

                string result = System.Text.Encoding.UTF8.GetString(response);

                return result;

            }
        }

 



Leave a Comment:


Click here to register

Popular articles