Using Google reCaptcha v2 in Asp.Net

How to Use Google reCaptcha v2 in Asp.Net Websites

It is extremely common to use captcha in order to prevent spam on web pages where information is obtained from users. We can create the captcha system we will use ourselves, or we can use ready-made systems.

The most used of these ready systems is the reCAPTCHA interface offered by Google.

Google announced that it will no longer update the reCAPTCHA v1.0 product, which has been in use for a long time, and that the reCAPTCHA v2.0 version will be developed from now on. The reCAPTCHA v1.0 version will continue to work on the sites where it is applied, but new developments will not be benefited from.

There were versions of reCAPTCHA v1 prepared for Php, Asp.Net and other platforms. However, if we want to use the reCAPTCHA v2 version on Asp.Net sites, it is necessary to find different solutions.

First, let's perform the standard steps for using reCAPTCHA v.2:

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

  • Paste the following code wherever you want the reCaptcha v2 object to appear. The site key given to you should be written in the place where site-key is written.
<div class="g-recaptcha" data-sitekey="site-key"></div>

After these steps, the reCaptcha object will appear on your page, but it will not work because it is not compatible with Asp.Net.

There are different solutions to fix this problem, one of which is as follows:

Create a class in the App_Code folder of your Asp.Net site and edit it like this:

using System.Collections.Generic;
 
public class ReCaptcha
{
public bool Success { get; set; }
public List<string> ErrorCodes { get; set; }
 
public static bool Validate(string encodedResponse)
{
    if (string.IsNullOrEmpty(encodedResponse)) return false;
 
    var client = new System.Net.WebClient();
    var secret = "paste your Secret Key";
 
    if (string.IsNullOrEmpty(secret)) return false;
 
    var googleReply = client.DownloadString(string.Format("https://www.google.com/recaptcha/api/siteverify?secret={0}&response={1}", secret, encodedResponse));
 
    var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
 
    var reCaptcha = serializer.Deserialize<ReCaptcha>(googleReply);
 
    return reCaptcha.Success;
}
}

Finally, we can use our .cs code file as follows to check if the verification is successful and take actions accordingly. (For example, write these codes into the program for the click event of a button.)

var encodedResponse = Request.Form["g-Recaptcha-Response"];
var isCaptchaValid = ReCaptcha.Validate(encodedResponse);
 
if (isCaptchaValid)
{
   // actions if validation successful
}
else
{
   // if not valid
}

 

recaptcha v2 asp.net, how to use asp.net recaptcha v2.0, using asp net recaptcha 2, recaptcha v2.0 in asp.net application, using recaptcha 2 on asp.net site, asp.net compatible recaptcha v2.0, recaptcha v2 dotnet

EXERCISES

There are no examples related to this subject.



COMMENTS




Read 1333 times.

Online Users: 212



using-google-recaptcha-v2-in-asp-net