1

I've implemented google's recapthca v2(checkbox) on my website(ASP.NET MVC) locally and, it works 100%. When I tried to copy over project files to my live website, the capthca did not work...

It does render correctly and I can interact with the checkbox, but the validation ALWAYS fails... It is the exact same project as on my localhost how can this be?

And I did change the site and secret keys when i moved files over to live site.

View:

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

        <div class="submit-frm-btn">

            <button class="contact-btn bg-mediumBlue"
                    type="submit">
                Submit
            </button>

controller:

 public ActionResult SaveForm(ContactUs contactUsForm)
         {
            //getting and setting response from captha
            var response = Request["g-recaptcha-response"];
            string secretkey = "my_secret_key";
            var client = new WebClient();
         
            var result = client.DownloadString(string.Format("https://www.google.com/recaptcha/api/siteverify?secret=" + secretkey + "&response=" + response  +""));
            var obj = JObject.Parse(result);
            var status = (bool)obj.SelectToken("success");
            ViewData["Message"] = status ? "Google reCaptcha validation success." : "Google reCaptcha validation failed.";

            //status true  = captcha successful
            if (status)
            {
               
                if (ModelState.IsValid)
                {

When captcha is correct and modelstate is valid, is save contact to DB. Note that this code works locally, I can successfully validate the capthca but the validation always fails on my live site.

Thanks for any help...

Kyle
  • 51
  • 9
  • does google's response include any error codes? – Vvamp Jun 13 '22 at 12:05
  • @Vvamp how do I check? – Kyle Jun 13 '22 at 12:07
  • I dont see any error codes, the validation just comes back as false.. – Kyle Jun 13 '22 at 12:16
  • The response should have an 'error codes' variable, which could contain errors; https://developers.google.com/recaptcha/docs/verify#api-response – Vvamp Jun 13 '22 at 12:17
  • no error codes, get request comes back as 200 from google – Kyle Jun 13 '22 at 12:30
  • have you tried testing it in a seperate browser? – Vvamp Jun 13 '22 at 12:31
  • yes i Have, safari and microsoft edge, same issue – Kyle Jun 13 '22 at 12:36
  • I'm not sure then. I don't see anything obviously wrong here, sorry – Vvamp Jun 13 '22 at 12:38
  • Found something very stange now... to get the response from my live site i tested the api call in postman with the correct secrect key and response parameters. I got a success: true" back. but yet the front-end displays validation failed? but in the code above that seems to be impossible? how does this happen... – Kyle Jun 13 '22 at 14:10
  • anyoneplease ? :( – Kyle Jun 14 '22 at 08:29
  • If you print the url right before actually requesting it in your saveform function, are the variables set correctly? – Vvamp Jun 14 '22 at 08:32
  • Yes it is, I copied the api endpoint and parameters from my live site and tested the exact same request in postman, and the 'success' token came back as true.... but yet front-end displays validation failed? once agian, this works locally but not on live site, I have exhausted all my options and dont know what to do at this point... – Kyle Jun 14 '22 at 10:15
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/245591/discussion-between-vvamp-and-kyle). – Vvamp Jun 14 '22 at 10:19
  • still no resolution... if anyone can assist – Kyle Jun 20 '22 at 06:43

1 Answers1

0

Found the problem. Every time I copied the local project files to the server, I swapped the config files because of the connection strings... And exactly that was the problem.

I assume Recaptcha has written in values into the local config file. It was the last thing I expected because I did not download a package for ReCaptcha or anything... Rookie mistake I guess.

Hope this helps anyone in the future

Kyle
  • 51
  • 9