How to implement Google Recaptcha 2.0 in ASP Classic?

16.8k views Asked by At

I need help to implement the answer Google Recaptcha 2.0.

I've tried a few ways to recover the response after sending the form but not consigui get the answer True.

Follows the example I'm trying:

recaptcha_secret = "example45454sasa"

sendstring = _
"https://www.google.com/recaptcha/api/siteverify?" & _ 
"secret=" & recaptcha_secret & _
"&response=" & request.form("g-recaptcha-response")

Set objXML = Server.CreateObject("MSXML2.ServerXMLHTTP")
objXML.Open "GET", sendstring , false

objXML.Send()

if instr(objXML.responseText,"true") then
    response.write "yes"
else
    response.write "no"
end if

Second exmeplae wehre I using aspJSON1.17.asp library:

recaptcha_secret = "example45454sasa"

Set oJSON = New aspJSON

jsonstring = "https://www.google.com/recaptcha/api/siteverify?secret=" & recaptcha_secret & "&response=" & request.form("g-recaptcha-response") & ""

'Load JSON string
oJSON.loadJSON("" & jsonstring & "")

'Get single value
Response.Write oJSON.data("success") & ""

The two examples above return False or No.

How do I implement a way to check that Recaptcha were marked?

*reCaptcha Documentatiom

Thanks for yout attention!


In the case of Zam as in my example the response that appears on the screen is:

Response: { "success": false, "error-codes": [ "invalid-input-secret" ] }

I believe it should appear "True" since I am answering the question correctly.

You can test: bit.ly/1R1cbEs

3

There are 3 answers

11
Zam On BEST ANSWER

I don't see how you send request.

Anyway, below is working sample with my site key for test web site. Of course, you should provide your own "secret key" and "data-sitekey"

Live sample: http://1click.lv/googlecaptcha.asp

File name: GoogleCaptcha.asp

<%@LANGUAGE=VBSCRIPT%>
<%
    Option Explicit
%>
<html>
    <head>
        <script src="https://www.google.com/recaptcha/api.js" async defer></script>
    </head>

    <body>
        <h4>http://stackoverflow.com/questions/30711884/how-to-implement-google-recaptcha-2-0-in-asp-classic/30735079</h4>
<%
    If Request.ServerVariables("REQUEST_METHOD") = "POST" Then
        Dim recaptcha_secret, sendstring, objXML
        ' Secret key
        recaptcha_secret = "6LfUUwgTAAAAAMQy5tz9u1BMSnCQV1CVh5tuBcEF"

        sendstring = "https://www.google.com/recaptcha/api/siteverify?secret=" & recaptcha_secret & "&response=" & Request.form("g-recaptcha-response")

        Set objXML = Server.CreateObject("MSXML2.ServerXMLHTTP")
        objXML.Open "GET", sendstring, False

        objXML.Send

        Response.write "<h3>Response: " & objXML.responseText & "</h3>"

        Set objXML = Nothing
    End If
%>

        <form method="post" action="GoogleCaptcha.asp">
            <!-- Site key -->
            <div class="g-recaptcha" data-sitekey="6LfUUwgTAAAAAAQZPb6j22A2a2tZoAUygdmqpgdv"></div>
            <br />
            <input type="submit" value="Try">
        </form>
    </body>
</html>
2
megyptm On

finally found it after so many search i found it first i use this asp class https://github.com/zhennanzhuce/js/blob/master/js/ueditor/1.4.3/asp/ASPJson.class.asp then to validate the response i use this

    result = (objXML.responseText)

    Set oJSON = New aspJSON
    oJSON.loadJSON(result)

    Set objXML = Nothing

    success = oJSON.data("success")
    if success = "True" then

        action = "go to next page"

    else

        action = ""


    end if

now the used code is :

<%@LANGUAGE=VBSCRIPT%>
<%
Option Explicit
%>
<html>
 <head>
    <script src="https://www.google.com/recaptcha/api.js" async defer></script>
</head>

<body>
<!-- #include file="aspJSON.asp"-->

<%
If Request.ServerVariables("REQUEST_METHOD") = "POST" Then
    Dim recaptcha_secret, sendstring, objXML
    ' Secret key
    recaptcha_secret = "XXXCCCXXXX"

    sendstring = "https://www.google.com/recaptcha/api/siteverify?onload=onloadCallback&render=explicit&secret=" & recaptcha_secret & "&response=" & Request.form("g-recaptcha-response")

    Set objXML = Server.CreateObject("MSXML2.ServerXMLHTTP")
    objXML.Open "GET", sendstring, False

    objXML.Send
    dim result, oJSON, success, action, errorCapatcha, errorMsg 

    result = (objXML.responseText)

    Set oJSON = New aspJSON
    oJSON.loadJSON(result)

    Set objXML = Nothing

    success = oJSON.data("success")
    if success = "True" then

        action = "go to next page"

    else

        action = "do nothing"


    end if

 END If

%>

    <form method="post" action="">
        <!-- Site key -->
        <div class="g-recaptcha" data-sitekey="XXXXXXXXX"></div>
        <br />
        <input type="submit" value="Try">
    </form>
</body>

0
MrKekson On

Here, it's a but if a butchery since the js+vb, but it's doing the job:

<script language=JavaScript RUNAT=SERVER src="json2.min.js"></script>
<script language=JavaScript RUNAT=SERVER >
    function gResult( jobj ){
        if ( JSON.parse( jobj ).success == true ){ return true }else{ return false }    
    }
</script>
<%

dim secret, objXmlHttp, VarString

secret = "yoursekretcodehere"

VarString = "secret=" & secret &_
            "&response=" & Request.Form("g-recaptcha-response") &_
            "&remoteip=" & Request.ServerVariables("REMOTE_ADDR")

Set objXmlHttp = Server.CreateObject("Msxml2.ServerXMLHTTP")

if isNull(objXmlHttp) then
    Set objXmlHttp = Server.CreateObject("Microsoft.XMLHTTP")
end if

objXmlHttp.open "POST", "https://www.google.com/recaptcha/api/siteverify?" & VarString , False
objXmlHttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
objXmlHttp.send

Set objXmlHttp = Nothing

 response.write gResult( objXmlHttp.responseText ) 

Ok now. Some explanation: first the given google link with the msxmlhttp lib will throw us back with "invalid-site-private-key", as one can see in a browser, I'm not sure why, but it always toss my requests with this error.

So i changed it to /siteverify, where we get some json errors. But if we post in the proper values, constructed in VarString( please note that "g-recaptcha-response" is the proper handler ), then it will pass a json back, with the result in it, but of course VB script is not built with such capabilities, so we need to do a bit of JS witchery. First, get json2.js, just google it, then include it to your src(href, or local copy), and a simple js function, to return with the proper response from G.

Then on the last line i just printed the result out, but one can do whatever one want with it.