question

robs23-9535 avatar image
0 Votes"
robs23-9535 asked robs23-9535 answered

reCaptcha implementation example for Razor Pages

Hello,

Just a quick question: can someone support me with example on how to implement reCaptcha verification in Razor Pages? I mean specifically Razor Pages, not MVC. Unfortunately, due to unfortunate naming (razor pages are also part of Asp.net MVC setup) I'm getting all the examples for MVC and they don't work for me.. I wish Microsoft would change the confusing name, but I'm guessing it's off the table now..
I've been trying to implement it with this plugin but the captcha isn't displayed (I get only captcha sign in the right corner saying that the site is protected, but still can submit form without any captcha validation).. The answer doesn't have to use the plugin, any working solution is welcome! :)


dotnet-aspnet-core-razor
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

The GitHub source code you shared contains a Razor Pages reCaptcha example.

 using Microsoft.AspNetCore.Mvc;
 using Microsoft.AspNetCore.Mvc.RazorPages;
    
 namespace AspNetCore.ReCaptcha.Net50.Pages.Home
 {
     [ValidateReCaptcha]
     public class ContactModel : PageModel
     {
         [BindProperty]
         public string Name { get; set; }
    
         [BindProperty]
         public string Body { get; set; }
    
         public void OnGet()
         {
         }
    
         public IActionResult OnPost()
         {
             if (!ModelState.IsValid)
                 return Page();
    
             TempData["Message"] = "Your form has been sent!";
             return RedirectToPage();
         }
     }
 }



0 Votes 0 ·
robs23-9535 avatar image
0 Votes"
robs23-9535 answered

With a help of packet's author I figured out that no "I'm not a robot" chekckbox appears by the design of V3 captchas - their only indication is the badge and no additional task for a user to prove he's not a robot is needed..

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

robs23-9535 avatar image
0 Votes"
robs23-9535 answered

@AgaveJoe, can't publish "reply" to your answer for some reason, so I'm posting it as separate answer..

Thanks, I must have overlooked that.. Honestly speaking I had it mostly like this, added necessary changes, but result is still the same - no "captcha" element is visiable on the page to tick "I'm not a robot" checkbox.. When I inspect it in Chrome's dev tools I can see some element has been generated:

 <div class="form-group">
 <input id="g-recaptcha-response" name="g-recaptcha-response" type="hidden" value="03AGdBq25La3sDHMHZvQjvAKHnUg720REb_GnVxuhh6FKU4TeYwgcjdNxsKJA1CY8fw6hdSprySH_23Cy-ryX0I-aJTpUfnCKPq8E6jpsQ5x7_jvTjId4OQoFMAj3FtNLYD9wB3znlI2AMs59bp5U9xg_3tsA8Nv_UZCn_TFUxYqKuaomxes6p9bAjCimoQ63o2AXA2ubqaNGFNsvxOz4I84wMGbfyM5PT0_7DIIgJXGo1t1itnYvGGYifLahgRO6-0_knnxzZUfktb2UB5nxK9YYmBBmJE0XiVJV7P9MHoKHagwOVn8nkhFqDHNt3H5opMIHADe0uUyWNc5Olv3abkq8h7grPul2nf3W52ryRS5GkCCjg20NRRKm0hzD4Kt-F2Lkym5QXv5PveLiW3w5vxtA4fUj8dEgdxRJ02S-04FS0u4ggwDtMhLNKxyLQfbunotPzv6HBZqXm"><script src="https://www.google.com/recaptcha/api.js?render=6LdxXcMbAAAAAH2wQWM7E1CUW1M7tPHTnu2s3GCK&amp;hl=en"></script><script>function updateReCaptcha() {grecaptcha.execute('6LdxXcMbAAAAAH2wQWM7E1CUW1M7tPHTnu2s3GCK', {action: 'homepage'}).then(function(token){document.getElementById('g-recaptcha-response').value = token;});}grecaptcha.ready(function() {setInterval(updateReCaptcha, 100000); updateReCaptcha()});</script>
                 </div>

but for some reason I cannot see it on the page :( Perhaps you'd like to check it yourself - here's the page and here's its source code.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.