Welcome to WebmasterWorld Guest from

Forum Moderators: ocean10000

Message Too Old, No Replies

Text To Image

5:54 pm on Apr 22, 2011 (gmt 0)

Junior Member

5+ Year Member

joined:Dec 4, 2009
posts: 186
votes: 0

I have a client that wants to implement Captcha of some kind on a simple contact form. I have no access to the server other than FTP'ing up files created in a text editor.

I've found several examples online but all seem to have logic or code flaws (surely not operator error!) so I've decided since it just needs to be simple, I will add it myself...or so I thought!

I have found code here and there for CREATING a graphic from text but I'm a bit hazy on how to DISPLAY that graphic on my page.

Because some parts of the site are still wonky that are not related to this, I will basically be doing this in notepad so no way to add things to the App_Code folder etc.

Could someone help me out with a fairly simple example of displaying text as graphic on a page, both what would go in the code behind and on the actual page.
8:58 pm on Apr 22, 2011 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Nov 12, 2002
votes: 0

Can you just implement ReCaptcha? I can't imagine why there's a need to reinvent the wheel.
9:33 pm on Apr 22, 2011 (gmt 0)

Junior Member

5+ Year Member

joined:Dec 4, 2009
posts: 186
votes: 0

Maybe I'm missing something but to do ReCaptcha with ASP.NET, I think you have to have a DLL installed on the server...or am I missing something?

I have the key and the JS to show the recaptcha, but I cannot figure out how to get the value of it into my codebehind.
12:44 am on Apr 23, 2011 (gmt 0)

Junior Member

5+ Year Member

joined:Dec 4, 2009
posts: 186
votes: 0

I finally got the Captcha to work. Maybe I am missing something obvious but I couldn't find a clear example of getting this to work, except by using the DLL. I finally pieced it together. For the sake of completeness and posterity :-) I'm going to post what I did.

First I applied for my API key, used the global because i have 4 sites to use this on.

In the form/web page I added this code:
 <script type="text/javascript"
<iframe src="http://www.google.com/recaptcha/api/noscript?k=mypublickey"
height="250" width="500" frameborder="0"></iframe><br>
<textarea name="x" rows="3" cols="40">
<input type="hidden" name="recaptcha_response_field"

Then in the button click I added this (right now it just sends the result back to a label but you could change as needed:

 string response1 = Request["recaptcha_response_field"];
string challenge1 = Request["recaptcha_challenge_field"];
string privatekey1 = "myprivatekey";
string remoteip1 = Request.ServerVariables["REMOTE_ADDR"];

Uri address = new Uri("http://www.google.com/recaptcha/api/verify");

// Create the web request
HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest;

// Set type to POST
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";

// Create the data we want to send
StringBuilder data = new StringBuilder();
data.Append("&response=" + HttpUtility.UrlEncode(response1));
data.Append("&challenge=" + HttpUtility.UrlEncode(challenge1));
data.Append("&privatekey=" + HttpUtility.UrlEncode(privatekey1));
data.Append("&remoteip=" + HttpUtility.UrlEncode(remoteip1));

// Create a byte array of the data we want to send
byte[] byteData = UTF8Encoding.UTF8.GetBytes(data.ToString());

// Set the content length in the request headers
request.ContentLength = byteData.Length;

// Write data
using (Stream postStream = request.GetRequestStream())
postStream.Write(byteData, 0, byteData.Length);

// Get response
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
// Get the response stream
StreamReader reader = new StreamReader(response.GetResponseStream());

// Console application output
Label1.Text = reader.ReadToEnd();

Of course replace mypublickey and mypricatekey with your keys. YMMV...hope it helps someone though!