CopyPastehas never been so tasty!

A CAPTCHA Image / control in ASP.NET 3.5

by anonymous

  • 0
  • 0
  • 0
111 views

A CAPTCHA Image / control in ASP.NET 3.5

By Using the code:

 

CaptchaHandler.ashx file code:

<%@ WebHandler Language="C#" %>

 

using System;

using System.Web;

using System.Drawing;

using System.IO;

using System.Web.SessionState;

 

public class CaptchaHandler : IHttpHandler, IReadOnlySessionState

{

    public void ProcessRequest (HttpContext context) {

        Bitmap bmp = new Bitmap(200, 50);

        Graphics grps = Graphics.FromImage(bmp);

        grps.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;

        grps.FillRectangle(Brushes.Green, 0, 0, 100, 20);

        grps.DrawString(context.Session["CaptchaHandler"].ToString(), new Font("Arial", 10), new SolidBrush(Color.Red), 0, 0);

        MemoryStream mstream = new MemoryStream();

        bmp.Save(mstream, System.Drawing.Imaging.ImageFormat.Png);

        byte[] bmpBytes = mstream.GetBuffer();

        bmp.Dispose();

        mstream.Close();

        context.Response.BinaryWrite(bmpBytes);

        context.Response.End();

    }

    public bool IsReusable {

        get {

            return false;

        }

    }

}

.cs file code:

using System;

using System.Collections;

using System.Configuration;

using System.Data;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.HtmlControls;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

 

public partial class Default13 : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

        if (!IsPostBack)

        {

            SetVerTxt();

        }

    }

    public void SetVerTxt()

    {

        Random rand = new Random();

        int n = rand.Next();

        Session["CaptchaHandler"] = n.ToString();

    }

    protected void CValidate(object source, ServerValidateEventArgs args)

    {

        if (Session["CaptchaHandler"] != null)

        {

            if (TextBox1.Text != Session["CaptchaHandler"].ToString())

            {

                SetVerTxt();

                args.IsValid = false;

                return;

            }

        }

        else

        {

            SetVerTxt();

            args.IsValid = false;

            return;

        }

 

    }

    protected void Button1_Click(object sender, EventArgs e)

    {

        if (!Page.IsValid)

        {

            return;

        }

        Response.Write("Verified!");

        SetVerTxt();

    }

}

Default.aspx

This is a very simple sample web form that contains only a few basic elements

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default13.aspx.cs" Inherits="Default13" %>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title>Untitled Page</title>

</head>

<body>

   <form id="form1" runat="server">

<div>

      <asp:CustomValidator ID="CustomValidator1" runat="server" ControlToValidate="TextBox1"

            ErrorMessage="Wrong Verification Code!" OnServerValidate="CValidate"></asp:CustomValidator>      

     <asp:Image ID="imageCaptcha" ImageUrl="CaptchaHandler.ashx" runat="server" />

        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"  ControlToValidate="TextBox1"

          ErrorMessage="Enter Code From Picture Plz!" ></asp:RequiredFieldValidator>

        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Save" /></div>

 

</form>

</body>

</html>

Add A Comment: