Click here to Skip to main content
15,303,522 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have implemented V2 reCAPTCHA and V3 reCAPTCHA in single webform using asp.net. V2 reCAPTCHA is working fine but for v3 i added attributes such as g-recaptcha class and data site key in asp.net button then on click of that button postback is not happening means it is not loading to next page. but token is generating on client side. Below I have added my code please can anyone give me solution.

What I have tried:

ASP.NET
<asp:Button class="btn-submit submit btn btn-primary pull-right g-recaptcha" ID="butSubmit"
                                runat="server" Text="SIGN IN" OnClick="butSubmit_Click" 
                                    data-callback='onSubmit' data-action='submit' UseSubmitBehavior="true"></asp:Button>




Java script:
JavaScript
function onSubmit(token) {
        debugger;
       document.getelementbyid("frm").submit();
        console.log(token);
        gcaptcha();
    }
    
    function gcaptcha() {
        debugger;
        if ($('#hdnrecaptcha').val() == "true") {
            debugger;
            if ($('#Hiddencaptchav2').val() == "true") {
                $('#recaptcha').show();
                $('#recaptchaerror').show();
                //submitUserForm();
    
            }
    
           
            else if ($('#Hiddencaptchav3').val() == "true") {
                    debugger;
                    grecaptcha.ready(function () {
                        debugger;
                        grecaptcha.execute('<%=ConfigurationManager.AppSettings["v3SiteKey"].ToString() %>', { action: 'submit' }).then(function (token) {
                            $('#token').val(token);
                            console.log(token);
                        });
                    });
                }
            }
        }
    
    
        
            
        
    
    //check v2 response
    var recaptcha_response = '';
    function submitUserForm() {
        debugger;
        if ($('#hdnrecaptcha').val() == "true") {
            debugger;
            if ($('#Hiddencaptchav2').val() == "true") {
                debugger;
                if (recaptcha_response.length == 0) {
                    console.log(recaptcha_response.length);
                    //alert("Iam not robot");
                    document.getElementById('recaptchaerror').innerHTML = '<span style="color:red;">This field is required.refresh the page</span>';
                    return false;
                }
            }
        }
        return true;
    }
    
    //verify captcha v2
    function verifyCaptcha(token) {
        console.log("verified");
        alert("captcha verified");
        recaptcha_response = token;
        console.log(token);
        document.getElementById('recaptchaerror').innerHTML = '';
    }
    
    //v2 onload
    function onload() {
        var onloadCallback = function () {
            /*var v2token = '<%=ConfigurationManager.AppSettings["SiteKey"].ToString() %>'*/
                    grecaptcha.render('recaptcha', {
                        'sitekey': '<%=ConfigurationManager.AppSettings["SiteKey"].ToString() %>'
                    });
                };
            }





v3 recaptcha method:
ASP.NET
public bool V3ReCaptchValid()
                {
                    try
                    {
                        Console.WriteLine("hi");
                        bool result = false;
                        string captchaResponse = Request.Form["g-recaptcha-response"];
                       // string captchaResponse = ConfigurationManager.AppSettings["v3SiteKey"];
                        //bool result = false;
                        var secretKey = ConfigurationManager.AppSettings["v3SecretKey"];
                        var apiUrl = ConfigurationManager.AppSettings["Url"] + "?" + "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");
                                Console.WriteLine(isSuccess);
                                result = (bool)(isSuccess) ? true : false;
                            }
                        }
        
                        return result;
                    }
                    catch (Exception ex)
                    {
                        
                    }
                    return false;
                }




here I am having issue with postback after clicking on submit button it is not hitting to next page. I have set data site key for button in server side. Can anyone give me
solution for this.
Posted
Updated 31-Dec-21 2:08am
v2
Comments
CHill60 31-Dec-21 8:09am
   
This code section will not help at all
catch (Exception ex)                    {  

                                          
}
You are just "swallowing" any exceptions thrown. Do something useful with the Exception (e.g. report it or log it) OR get rid of the Try-Catch around your code. Then you might actually find out why it isn't working

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900