albx / KITT.Web.ReCaptcha

A reCaptcha implementation for ASP.NET Core
MIT License
3 stars 1 forks source link

Having an error with reCAPTCHA v3 on a Blazor Server project #31

Closed zawarq closed 5 months ago

zawarq commented 6 months ago

I followed the instructions for reCAPTCHA v3 (Blazor Server). When executing the following line in SubmitAsync:

var result = await ReCaptcha.VerifyAsync(action: "submit");

The variable result contains the following error:

grecaptcha is not defined ReferenceError: grecaptcha is not defined at https://localhost:7233/_content/KITT.Web.ReCaptcha.Blazor/v3/recaptcha-v3.js:3:9 at new Promise () at Module.execute (https://localhost:7233/_content/KITT.Web.ReCaptcha.Blazor/v3/recaptcha-v3.js:2:12) at https://localhost:7233/_framework/blazor.web.js:1:3244 at new Promise () at y.beginInvokeJSFromDotNet (https://localhost:7233/_framework/blazor.web.js:1:3201) at fn._invokeClientMethod (https://localhost:7233/_framework/blazor.web.js:1:62869) at fn._processIncomingData (https://localhost:7233/_framework/blazor.web.js:1:60344) at connection.onreceive (https://localhost:7233/_framework/blazor.web.js:1:53985) at i.onmessage (https://localhost:7233/_framework/blazor.web.js:1:82130)

Please note that I had also added the component tag in my App.razor

albx commented 6 months ago

Hi @zawarq ...I'm just trying to figure out where the problem could be. I'm currently review all the samples to use the new Blazor Web App with "Auto" interactivity mode and my code seems working. May I ask you to try to put the component above the Blazor javascript file as in this example:

<ReCaptchaScript />
<script src="_framework/blazor.web.js"></script>

If in this way your problem is solved, this is anyway a good point of lack of documentation, so in any case this issue to me is a bug to be solved in some way (code or documentation).

Many thanks!

zawarq commented 6 months ago

Hi

I made the changes you suggest and still get the same error. Please note that my application is set to the render mode InteractiveServer through:

<Routes @rendermode="InteractiveServer" />

Do you think this could be causing the issue? The application is needed to be in this mode for certain functionality to work

albx commented 6 months ago

Honestly I don't know. In the following days I will try to setup a sample with global server interactivity as in your use case and try to reproduce this issue.

zawarq commented 6 months ago

Thanks! I appreciate your efforts in solving this

albx commented 6 months ago

Hi @zawarq, finally I have tried to reproduce the problem. I created a Blazor Web App new project with global Server interactivity but everything seems working to me. I pushed the sample in this branch: https://github.com/albx/KITT.Web.ReCaptcha/tree/bug/31-error-v3-blazor-server/samples/v3/KITT.Web.ReCaptcha.Samples.v3.BlazorServerInteractivity

Maybe you could start from there to compare your solution and try to understand if there are some differences. Thank you

zawarq commented 5 months ago

Thanks @albx ! Changing Program.cs according to this sample did the job!

albx commented 5 months ago

@zawarq Good! So the problem was the components registration on the Program.cs? I keep this issue open in order to improve documentation for this use case.

Thank you for your contrib!

albx commented 5 months ago

Closing with PR #33