HOW TO USE CAPTCHA IN ASP.NET Web Forms
In this post , I am going to explain and demonstrate how to
use captcha in ASP.NET with c#.
Captcha is use to check whether the end user is a human
being or a robot which tries to busy your application and unnecessary consumes
the data traffic line so that your Application’s performance can be degraded.
Here
are steps how to use captcha in our application.
Step 1: Make a class file for creating captcha in C#-
C# Code
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
using System.Drawing.Imaging;
using System.Text;
public partial class CaptchaControl : System.Web.UI.Page
{
private Random rand = new Random();
protected void Page_Load(object sender, EventArgs e)
{
CreateImage();
}
private void CreateImage()
{
string code = GetRandomText();
Bitmap bitmap = new Bitmap(200,70,System.Drawing.Imaging.PixelFormat.Format32bppArgb);
Graphics g = Graphics.FromImage(bitmap);
Pen pen = new Pen(Color.Green);
Rectangle rect = new Rectangle(0,0,200,70);
SolidBrush b = new SolidBrush(Color.DarkKhaki);
SolidBrush blue = new SolidBrush(Color.Blue);
int counter = 0;
g.DrawRectangle(pen, rect);
g.FillRectangle(b, rect);
for (int i = 0; i < code.Length; i++)
{
g.DrawString(code[i].ToString(), new Font("Verdena", 10 +
rand.Next(14, 18)), blue, new PointF(10 + counter, 10));
counter += 20;
}
DrawRandomLines(g);
bitmap.Save(Response.OutputStream,ImageFormat.Jpeg);
g.Dispose();
bitmap.Dispose();
}
private void DrawRandomLines(Graphics g)
{
SolidBrush green = new SolidBrush(Color.Green);
for (int i = 0; i < 20; i++)
{
g.DrawLines(new Pen(green, 2),
GetRandomPoints());
}
}
private Point[] GetRandomPoints()
{
Point[] points = { new Point(rand.Next(10, 150), rand.Next(10, 150)), new Point(rand.Next(10, 100),
rand.Next(10, 100)) };
return points;
}
private string GetRandomText()
{
StringBuilder randomText = new StringBuilder();
if (Session["Code"] == null)
{
string alphabets = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789";
Random r = new Random();
for (int j = 0; j <= 5; j++)
{
randomText.Append(alphabets[r.Next(alphabets.Length)]);
}
Session["Code"] =
randomText.ToString();
}
return Session["Code"] as String;
}
}
Step 2: Now we have code that can create captcha. Now we are
going how to using captcha wherever this is required in our application.
For this, We have added a new page in our application then
use the following code-
Default.aspx Page
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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>Captcha Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Image ID="myImage" runat="server" ImageUrl="~/CaptchaControl.aspx" />
<br />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
<br />
</div>
</form>
</body>
</html>
Default.aspx.cs Code
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class _Default : System.Web.UI.Page
{
protected void Button1_Click(object sender, EventArgs e)
{
if (Session["Code"].ToString().Equals(TextBox1.Text))
{
Response.Write("You are not a BOT.");
}
else
{
// clear
the session and generate a new code
Session["Code"] = null;
Response.Write("Hey! You are a BOT.");
}
}
}
Now run the application, we get the following screen where
we can Check whether the entered captcha is correct or not. Here is the screen
shots of the demo application.
Download The Sample Project: Download Here
Written By: S. Vikash Chandel

No comments:
Post a Comment