Recaptcha issue RRS feed

  • Question

  • User-1767698477 posted

    <p>Well, I found some code on a site and this is not working for me with the api key. It will not allow the form to be put through without answering the captcha question, but even when you answer it correctly, it gives the valid code error message.</p>

    also added to web.config under appsettings:

    <add key="Google.ReCaptcha.Secret" value="removed " />

    Can someone tell me when it's not proceeding to send the email?

    Public Class ReCaptcha
    Public Property Success As Boolean
    Public Property ErrorCodes As List(Of String)

    Public Shared Function Validate(ByVal encodedResponse As String) As Boolean
    If String.IsNullOrEmpty(encodedResponse) Then Return False
    Dim client = New System.Net.WebClient()
    Dim secret = ConfigurationManager.AppSettings("Google.ReCaptcha.Secret")
    If String.IsNullOrEmpty(secret) Then Return False
    Dim googleReply = client.DownloadString(String.Format("https://www.google.com/recaptcha/api/siteverify?secret={0}&response={1}", secret, encodedResponse))
    Dim serializer = New System.Web.Script.Serialization.JavaScriptSerializer()
    Dim reCaptcha = serializer.Deserialize(Of ReCaptcha)(googleReply)
    Return reCaptcha.Success
    End Function
    End Class

    Partial Public Class contact
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)

    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)

    If Page.IsValid Then
    Dim EncodedResponse As String = Request.Form("g-Recaptcha-Response")
    Dim IsCaptchaValid As Boolean = IIf(ReCaptcha.Validate(EncodedResponse) = "True", True, False)

    If IsCaptchaValid Then
    'Valid Request
    'send email
    lblResult.Text = ""
    pnlMsg.Visible = True
    Dim message As String = tbRequest.Text
    Dim name As String = tbName.Text
    Dim email As String = tbEmail.Text
    SendSimpleMail(email, "Inquiry at pacificwestcapital.net", name, message, email)

    lblResult.Text = "Incorrect code, please retry."
    lblResult.ForeColor = System.Drawing.Color.Red
    End If
    End If
    End Sub

    Sunday, December 16, 2018 4:34 AM


  • User283571144 posted

    Hi sking,

    According to your description, I have created a test demo on my side, it wroks well.

    I guess you may not use the right secret key or token.

    The secret key:

    I directly use client-side script to get the token and use jquery to store it into the hiddenfield.

    Then I use it send to the google api.

    More details, you could refer to below codes:

    <%@ Page Language="vb" AutoEventWireup="false" CodeBehind="TestForValidateGoogle.aspx.vb" Inherits="VBWebform.TestForValidateGoogle" %>
    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
            <script src='https://www.google.com/recaptcha/api.js?render=6LcsRoIUAAAAAO9IvmxxCTOeTbvRspZcJHqtjs_G'></script>
        <script src="Scripts/jquery-1.12.4.min.js"></script>
    grecaptcha.ready(function() {
    grecaptcha.execute('6LcsRoIUAAAAAO9IvmxxCTOeTbvRspZcJHqtjs_G', {action: 'homepage'})
        .then(function (token) {
        <form id="form1" runat="server">
                <asp:Button ID="Button1" runat="server" Text="Validate"  OnClick="Button1_Click" />
                <asp:HiddenField ID="HiddenField1" runat="server" />      


    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Net;
    using System.Text;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    namespace AspNetNormalIssue.Webform
        public partial class GoogleValidateTest : System.Web.UI.Page
            protected void Page_Load(object sender, EventArgs e)
            protected void Button1_Click(object sender, EventArgs e)
               var re =  CaptchaValidate();
            private Dictionary<bool, string> CaptchaValidate()
                // Validate Captcha here
                var isValidDic = new Dictionary<bool, string>();
                string[] resultFromGoogle;
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create("https://www.google.com/recaptcha/api/siteverify");
                req.ProtocolVersion = HttpVersion.Version10;
                 req.Method = "POST";
                req.ContentType = "application/x-www-form-urlencoded";
                string Fdata = string.Format("secret={0}&response={1}",
                    new object[]{
                byte[] resData = Encoding.ASCII.GetBytes(Fdata);
                using (Stream rStream = req.GetRequestStream())
                    rStream.Write(resData, 0, resData.Length);
                    using (WebResponse wResponse = req.GetResponse())
                        using (TextReader readStream = new StreamReader(wResponse.GetResponseStream(), Encoding.UTF8))
                            resultFromGoogle = readStream.ReadToEnd().Split(new string[] { "\n", @"\n" }, StringSplitOptions.RemoveEmptyEntries);
                catch (WebException ex)
                    isValidDic.Add(false, ex.InnerException.ToString());
                    return isValidDic;
                isValidDic.Add(resultFromGoogle[0] == "true", resultFromGoogle[1]);
                return isValidDic;


    Best Regards,


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, December 17, 2018 7:06 AM