UncleCoder.com

UncleCoder.com

Free programming examples and instructions

Validating recaptcha in Update Panel - Asp.net

Demo and Code for how to validate recaptcha 2.0 in Update panel -Asp.net

by Athil


Posted on 27 Jul 2018 Category: Asp.net Views: 3251


Here I am going to show how to validate ReCaptcha 2 that is placed in updatepanel.

DEMO

Before that, I posted one article how to validate ReCaptcha 2 in ASP.Net The same way you can bring ReCaptcha. and bring it in a updatepanel.

ASPX Page

 <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdateContact" runat ="server">
   <ContentTemplate>
 ............
 ............
 <div id="recaptcha" class="g-recaptcha" data-sitekey="6Lf6ahMU************************"></div>
 
 ..........
 ..........

 </ContentTemplate>
</asp:UpdatePanel>

Name space in Server side

using Newtonsoft.Json.Linq;

C# Code

protected void Button1_Click(object sender, EventArgs e)
{
     string EncodedResponse = Request.Form["g-Recaptcha-Response"];
     string recaptchRespone = recaptchaPost(EncodedResponse);
     dynamic json = JObject.Parse(recaptchRespone);
     if (json.success == true)
     {
         //Success
     }
     else
     {
          // Failed
     }
     ScriptManager.RegisterStartupScript(UpdateContact, UpdateContact.GetType(), "loadCaptcha", "grecaptcha.render('recaptcha', {'sitekey': '6Lf6ahMUAAAAAHLm792fEkmaO9Zs5wtevupniRz0' });", true);
}
public string recaptchaPost(string EncodedResponse)
{
    using (WebClient client = new WebClient())
    {
         byte[] response =
         client.UploadValues("https://www.google.com/recaptcha/api/siteverify", new NameValueCollection()
                   {
                       { "secret", "6Lf6ahM******************************" },
                       { "response", EncodedResponse }

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

 



Leave a Comment:


Click here to register

Popular articles