Tuesday 12 December 2017

How to Integrate “No CAPTCHA reCAPTCHA” in Your Website

Note:
First, we need an API key, so head on over to https://www.google.com/recaptcha/admin. To gain access to this page you’ll need to be logged into a Google account. You’ll be asked to register your website, so give it a suitable name, then list domains (for example ashishsrivastava.info) where this particular reCAPTCHA will be used. Subdomains (such as blog.ashishsrivastava.info) are automatically taken into account


Code :
Example 1 :

Create Class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Newtonsoft.Json;
/// <summary>
/// Summary description for ReCaptchaClass
/// </summary>
public class ReCaptchaClass
{
    public static string Validate(string EncodedResponse)
    {
        var client = new System.Net.WebClient();

        string PrivateKey = "Your KEY";

        var GoogleReply = client.DownloadString(string.Format("https://www.google.com/recaptcha/api/siteverify?secret={0}&response={1}", PrivateKey, EncodedResponse));

        var captchaResponse = Newtonsoft.Json.JsonConvert.DeserializeObject<ReCaptchaClass>(GoogleReply);

        return captchaResponse.Success;
    }

    [JsonProperty("success")]
    public string Success
    {
        get { return m_Success; }
        set { m_Success = value; }
    }

    private string m_Success;
    [JsonProperty("error-codes")]
    public List<string> ErrorCodes
    {
        get { return m_ErrorCodes; }
        set { m_ErrorCodes = value; }
    }


    private List<string> m_ErrorCodes;
}

Using webservice

protected static string ReCaptcha_Key = "Your Key";
    protected static string ReCaptcha_Secret = "Your Secret";

    [WebMethod]
    public string VerifyCaptcha(string response)
    {
        string url = "https://www.google.com/recaptcha/api/siteverify?secret=" + ReCaptcha_Secret + "&response=" + response;
        return (new WebClient()).DownloadString(url);
    }


HTML PAGE
JS
<script src="https://www.google.com/recaptcha/api.js?onload=myCallBack&render=explicit" async defer></script>
or
<script src='https://www.google.com/recaptcha/api.js'></script>

<div class="g-recaptcha" data-sitekey="6LfWVw8UAAAAAFioLPix8GJ4dJC83PC6V7Kb26Fd"></div>

Validate using class

 string EncodedResponse = Request.Form["g-Recaptcha-Response"];
           
            bool IsCaptchaValid = (ReCaptchaClass.Validate(EncodedResponse) == "true" ? true : false);

            if (IsCaptchaValid)
            {
}
 else
            {
              /
                ScriptManager.RegisterStartupScript(this, this.GetType(), "msg", "javascript:alert('Please verify captcha');", true);
             
            }

Validate using Web Service
using Newtonsoft.Json;

MyService objMyService = new MyService();

 string EncodedResponse = Request.Form["g-Recaptcha-Response"];
            String json = objMyService.VerifyCaptcha(EncodedResponse);

            dynamic stuff = JsonConvert.DeserializeObject(json);
   if (stuff.success == "True")
{
}
            else
            {
             
               ScriptManager.RegisterStartupScript(this, this.GetType(), "msg", "javascript:alert('Please verify captcha');", true);
             

            }

No comments:

Post a Comment

Upload valid file in C#

    protected bool CheckFileExtandLength(HttpPostedFile HtmlDocFile)     {         try         {             Dictionary<string, byte[]>...