AspNetCore.ReCaptcha icon indicating copy to clipboard operation
AspNetCore.ReCaptcha copied to clipboard

Cannot validate V3 token via AJAX

Open DanHalford opened this issue 1 year ago • 1 comments

This is possibly (almost certainly) a result of my stupidity, but I'm unable to validate a V3 token when submitted via AJAX. Excuse terminology - I'm not a programmer...

On my Razor form, I have this:

var token = document.querySelector('input[name="__RequestVerificationToken"]').value;

fetch('/auth/register', {
    method: 'POST',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify({
        email: emailField.value,
        password: passwordField.value,
        token: token
    })
})

This is capturing the token and sending it back, as follows: image

However, any call to GetVerifyResponseAsync results in an invalid-input-response error code: image

The key and secret are correct - they're working on another page that uses a traditional form post submission.

Any ideas, or am I just terminally dumb?

DanHalford avatar Feb 14 '24 01:02 DanHalford

You're passing the ASP.NET Core Antiforgery token to the recaptcha service ("__RequestVerificationToken" is the antiforgery token, not the recaptcha token). You will need to get the value of input[name="g-recaptcha-response"] and send that as the token value instead.

sleeuwen avatar Jul 20 '24 14:07 sleeuwen