Currently working on integrating v3 recaptcha into my asp.net webforms page.
The issue I'm facing is that it seems like grecaptcha.ready operates asynchronously, causing the page to postback immediately without waiting for the client event to complete.
Is there a way to make the postback pause until grecaptcha.ready and grecaptcha.execute are finished?
Here's the front-end code snippet:
<script src="https://www.google.com/recaptcha/api.js?render=key"></script>
<script>
function onbtnRegistrazioneClick(e) {
return grecaptcha.ready(function () {
grecaptcha.execute('key', { action: 'submit' }).then(function (token) {
console.log(token);
document.getElementById("gRecaptchaResponse").value = token;
});
});
}
</script>
<asp:Button runat="server" ID="btnRegistrazioneCliente" CssClass="btnGenerico" Text='<%$ Resources:btnRegistratiText%>' ValidationGroup="NewUtente" CausesValidation="true" OnClientClick="onbtnRegistrazioneClick(event);"></asp:Button>
<input type="hidden" value="" id="gRecaptchaResponse" name="gRecaptchaResponse" />
And here's the back-end code snippet:
Public Sub btnRegistrazioneCliente_Click(sender As Object, e As System.EventArgs) Handles btnRegistrazioneCliente.Click 'btnRegistrazioneCliente.ServerClick '
If Me.IsValid Then
Dim gRecaptchaResponse As String = Request.Form("gRecaptchaResponse")
Dim isCaptchaValid As Boolean = Globals.Security.ReCaptchaV3.Validate(gRecaptchaResponse)
'...
else
'...
end if
End Sub