Captcha Image Validation

The captcha image control relies on simplicity; the server validation is just 1 line of code. The text sent to the imaage generation service is crypted with Rijndael.




Here the text arrangement is changed between posts; it has hard readness.

Here the text arrangement is preserved between postbacks; it has normal readness and it's rendered just from the chars ´abc345KLM´.

			<%@ Page Language="C#" MasterPageFile="~/Demo.master" AutoEventWireup="true" 
    Inherits="SharpPieces.DemoApp.CaptchaImgValidation" 
    CodeBehind="captchaimgvalidation.aspx.cs" %>

<asp:Content ID="Content2" runat="Server" ContentPlaceHolderID="headPlaceHolder">
    <title>SharpPieces - Captcha Image Validation - Live Demo</title>
</asp:Content>
<asp:Content ID="Content3" runat="Server" ContentPlaceHolderID="descriptionPlaceholder">
    <h1>Captcha Image Validation</h1>
    
    <p>The captcha 
        image control relies on simplicity; the server validation is just 1 line of code. 
        The text sent to the imaage generation service is crypted with Rijndael.</p>
</asp:Content>
    
<asp:Content ID="Content1" ContentPlaceHolderID="demoPlaceholder" Runat="Server">
    <asp:Label ID="Label1" runat="server" Text="Here the text arrangement is changed between posts; 
    it has hard readness."></asp:Label>
    <br />
    <piece:CaptchaImage ID="CaptchaImage1" runat="server" ReadnessLevel="Hard">
        <Text Font="ArialBlack, 22pt, style=Bold" HorizontalAlign="Center" VerticalAlign="Center" />
    </piece:CaptchaImage>
    <asp:TextBox ID="TextBox1" runat="server" Width="100px"></asp:TextBox>
    <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" 
        OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
        <asp:ListItem Text="5 chars" Value="5"></asp:ListItem>
        <asp:ListItem Text="4 chars" Value="4"></asp:ListItem>
        <asp:ListItem Text="3 chars" Value="3"></asp:ListItem>
    </asp:DropDownList>
    <asp:Label ID="Label2" runat="server" Text=""></asp:Label>
    <br />
    <asp:Label ID="Label3" runat="server" Text="Here the text arrangement is preserved between postbacks; 
    it has normal readness and it's rendered just from the chars �abc345KLM�."></asp:Label>
    <br />
    <piece:CaptchaImage ID="CaptchaImage2" runat="server" ServerCacheDuration="10" 
        ReadnessLevel="Normal" CharArray="abc345KLM">
        <Text Font="ArialBlack, 22pt, style=Bold" HorizontalAlign="Center" VerticalAlign="Center" />
    </piece:CaptchaImage>
    <asp:TextBox ID="TextBox2" runat="server" Width="100px"></asp:TextBox>
    <asp:Button ID="Button2" runat="server" Text="Reset text" OnClick="Button2_Click" />
    <asp:Label ID="Label4" runat="server" Text=""></asp:Label>
    <br />
    <asp:Button ID="Button1" runat="server" Text="Check" Width="70px" OnClick="Button1_Click" />
</asp:Content>
		
			using System;
using System.Web.UI;

namespace SharpPieces.DemoApp
{

    /// <summary>
    /// The Captcha Image Validation demo page.
    /// </summary>
    public partial class CaptchaImgValidation : Page
    {

        /// <summary>
        /// Handles the Click event of the Button1 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">
        /// The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Button1_Click(object sender, EventArgs e)
        {
            if (this.CaptchaImage1.IsValid(this.TextBox1.Text, true))
            {
                this.Label2.Text = "OK";
            }
            else
            {
                this.Label2.Text = "NOK";
            }

            if (this.CaptchaImage2.IsValid(this.TextBox2.Text, true))
            {
                this.Label4.Text = "OK";
            }
            else
            {
                this.Label4.Text = "NOK";
            }
        }

        /// <summary>
        /// Handles the SelectedIndexChanged event of the DropDownList1 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">
        /// The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.CaptchaImage1.TextLength = int.Parse(this.DropDownList1.SelectedValue);
            this.CaptchaImage1.ResetText();
        }
        /// <summary>
        /// Handles the Click event of the Button2 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">
        /// The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Button2_Click(object sender, EventArgs e)
        {
            this.CaptchaImage2.ResetText();
        }
    }

}