September 4, 2010
Home
|
Contact Us
are you a human?
Home
HIP Tests
Community
About Us
Contact Us
C#/ASP.NET Graphical Distorted Text Example
// Default.aspx <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
Bogus account creation page
Username:
Code:
// Default.aps.cs using System; using System.Collections; using System.Net; public partial class _Default : System.Web.UI.Page { /* * All requests end up here. If the 'code' and 'id' are present, * we assume the user is attempting to pass the CAPTCHA. If the * 'code' is not present, it's assumed we need to present the form. */ protected void Page_Load(object sender, EventArgs e) { if(Request.Form["form_id"] != null && Request.Form["form_code"] != null) { if(ValidateCode(Request.Form["form_id"], Request.Form["form_code"])) { captcha.Visible = false; form1.Visible = false; statusMsg.InnerText = "Account Created!"; } else { RefreshForm(); statusMsg.InnerText = "Incorrect Code"; } } else { RefreshForm(); } } private void RefreshForm() { form_id.Value = GenerateRandomKey(); form1.Visible = true; captcha.Src = "http://areyouahuman.org/captcha.pl?id=" + form_id.Value; captcha.Visible = true; } private string GetData(string url) { /* * This is a simple help to retrieve the reponse * from the CAPTCHA api. WebClient is declared in * System.Net */ WebClient wc = new WebClient(); string response = ""; response = wc.DownloadString(url); return response; } private bool ValidateCode(string id, string code) { /* * The 'code' and 'id' submitted by the user end up here. We * send them off to the CAPTCHA system for validation. If the * code matches, the CAPTCHA system responds with 'success'. * */ string url = String.Format("https://areyouahuman.org/captcha.pl?id={0}&code={1}&type=IMAGE", id, code); string reponse = GetData(url); if(reponse == "success") return true; else return false; } private string GenerateRandomKey() { /* * Generate a 32 byte alphanumeric key. This value * and format is required by the CAPTCHA system. * */ ArrayList alphabet = new ArrayList(); Random rand = new Random(); string key = ""; int i = 0; // 0 - 9 for(i = (int)'0'; i <= (int)'9'; i++) { alphabet.Add((char)i); } // a - z for(i = (int)'a'; i <= (int)'z'; i++) { alphabet.Add((char)i); } // A - Z for(i = (int)'A'; i <= (int)'Z'; i++) { alphabet.Add((char)i); } for(i = 0; i < 32; i++) { key += alphabet[rand.Next(0, alphabet.Count)]; } return key; } }