homepage Welcome to WebmasterWorld Guest from 54.243.12.156
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Subscribe to WebmasterWorld
Home / Forums Index / Microsoft / Microsoft IIS Web Server and ASP.NET
Forum Library, Charter, Moderators: ocean10000

Microsoft IIS Web Server and ASP.NET Forum

    
Text To Image
IntegrityWebDev




msg:4302632
 5:54 pm on Apr 22, 2011 (gmt 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.

 

mattglet




msg:4302720
 8:58 pm on Apr 22, 2011 (gmt 0)

Can you just implement ReCaptcha? I can't imagine why there's a need to reinvent the wheel.

IntegrityWebDev




msg:4302730
 9:33 pm on Apr 22, 2011 (gmt 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.

IntegrityWebDev




msg:4302790
 12:44 am on Apr 23, 2011 (gmt 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"
src="http://www.google.com/recaptcha/api/challenge?k=mypublickey">
</script>
<noscript>
<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">
</textarea>
<input type="hidden" name="recaptcha_response_field"
value="manual_challenge">
</noscript>


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!

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Microsoft / Microsoft IIS Web Server and ASP.NET
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About
© Webmaster World 1996-2014 all rights reserved