Step 1: Add a generic handler file in your web application and name it "CaptchaHandler.ashx". It will be used to create CAPTCHA image as a bitmap. Add following code in this file:
<%@ WebHandler Language="C#" Class="CaptchaHandler" %>
using System;
using System.Web;
using System.Drawing;
using System.Drawing.Text;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.IO;
using System.Web.SessionState;
namespace CustomControls
{
public class CaptchaHandler : IHttpHandler, IReadOnlySessionState
{
public void ProcessRequest (HttpContext context)
{
Bitmap objBmp = new Bitmap(75, 25);
Graphics objGraphics = Graphics.FromImage(objBmp);
objGraphics.SmoothingMode = SmoothingMode.AntiAlias;
objGraphics.Clear(Color.Gray);
objGraphics.TextRenderingHint = TextRenderingHint.AntiAlias;
Font objFont = new Font("Arial", 12, System.Drawing.FontStyle.Bold);
string strCaptcha = string.Empty;
if (context.Session["Captcha"].ToString() != null)
{
strCaptcha = context.Session["Captcha"].ToString();
}
objGraphics.DrawString(strCaptcha, objFont, Brushes.White, 3, 3);
MemoryStream ms = new MemoryStream();
objBmp.Save(ms, ImageFormat.Png);
byte[] bmpBytes = ms.GetBuffer();
context.Response.ContentType = "image/png";
context.Response.BinaryWrite(bmpBytes);
objBmp.Dispose();
objFont.Dispose();
objGraphics.Dispose();
ms.Close();
context.Response.End();
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
Step2: Now a class file in your web application and name it "Captcha.cs". This class file implements a custom web control which displays the bitmap CAPTCHA image created by the "CaptchaHandler.ashx" generic handler. Add following code in this file:
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace CustomControls
{
public class Captcha : Control
{
Image _imgCaptcha;
public string Text
{
get
{
if (HttpContext.Current.Session["Captcha"] != null)
{
return HttpContext.Current.Session["Captcha"].ToString();
}
else
{
return null;
}
}
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
string[] strArray = new string[36];
strArray = new string[] { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
Random autoRand = new Random();
string strCaptcha = string.Empty;
for (int i = 0; i < 6; i++)
{
int j = Convert.ToInt32(autoRand.Next(0, 62));
strCaptcha += strArray[j].ToString();
}
HttpContext.Current.Session.Add("Captcha", strCaptcha);
_imgCaptcha = new Image();
_imgCaptcha.ImageUrl = "~/CaptchaHandler.ashx";//Image URL is set to the generic handler created in Step1
this.Controls.Add(_imgCaptcha);
}
}
}
Step3: Now define a tag prefix for the created CAPTCHA web control. To do this, locate the <pages><controls> tags in the web.config file and add a tagprefix tag within these tags as below:
<pages>
<controls>
<add namespace="CustomControls" tagPrefix="cwc"/>
</controls>
</pages>
Step4: Now add the custom web control (created in step2) in the web page where CAPTCHA image needs to be displayed. Following code snippet can be used:
<cwc:Captcha ID="Captcha1" runat="Server" />
Use following code snippet to compare the CAPTCHA image text with the user entered text:
if(Captcha1.Text != UserTextBox.Text )
{
--Display error message--
}
else
{
--proceed to next--
}
<%@ WebHandler Language="C#" Class="CaptchaHandler" %>
using System;
using System.Web;
using System.Drawing;
using System.Drawing.Text;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.IO;
using System.Web.SessionState;
namespace CustomControls
{
public class CaptchaHandler : IHttpHandler, IReadOnlySessionState
{
public void ProcessRequest (HttpContext context)
{
Bitmap objBmp = new Bitmap(75, 25);
Graphics objGraphics = Graphics.FromImage(objBmp);
objGraphics.SmoothingMode = SmoothingMode.AntiAlias;
objGraphics.Clear(Color.Gray);
objGraphics.TextRenderingHint = TextRenderingHint.AntiAlias;
Font objFont = new Font("Arial", 12, System.Drawing.FontStyle.Bold);
string strCaptcha = string.Empty;
if (context.Session["Captcha"].ToString() != null)
{
strCaptcha = context.Session["Captcha"].ToString();
}
objGraphics.DrawString(strCaptcha, objFont, Brushes.White, 3, 3);
MemoryStream ms = new MemoryStream();
objBmp.Save(ms, ImageFormat.Png);
byte[] bmpBytes = ms.GetBuffer();
context.Response.ContentType = "image/png";
context.Response.BinaryWrite(bmpBytes);
objBmp.Dispose();
objFont.Dispose();
objGraphics.Dispose();
ms.Close();
context.Response.End();
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
Step2: Now a class file in your web application and name it "Captcha.cs". This class file implements a custom web control which displays the bitmap CAPTCHA image created by the "CaptchaHandler.ashx" generic handler. Add following code in this file:
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace CustomControls
{
public class Captcha : Control
{
Image _imgCaptcha;
public string Text
{
get
{
if (HttpContext.Current.Session["Captcha"] != null)
{
return HttpContext.Current.Session["Captcha"].ToString();
}
else
{
return null;
}
}
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
string[] strArray = new string[36];
strArray = new string[] { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
Random autoRand = new Random();
string strCaptcha = string.Empty;
for (int i = 0; i < 6; i++)
{
int j = Convert.ToInt32(autoRand.Next(0, 62));
strCaptcha += strArray[j].ToString();
}
HttpContext.Current.Session.Add("Captcha", strCaptcha);
_imgCaptcha = new Image();
_imgCaptcha.ImageUrl = "~/CaptchaHandler.ashx";//Image URL is set to the generic handler created in Step1
this.Controls.Add(_imgCaptcha);
}
}
}
Step3: Now define a tag prefix for the created CAPTCHA web control. To do this, locate the <pages><controls> tags in the web.config file and add a tagprefix tag within these tags as below:
<pages>
<controls>
<add namespace="CustomControls" tagPrefix="cwc"/>
</controls>
</pages>
Step4: Now add the custom web control (created in step2) in the web page where CAPTCHA image needs to be displayed. Following code snippet can be used:
<cwc:Captcha ID="Captcha1" runat="Server" />
Use following code snippet to compare the CAPTCHA image text with the user entered text:
if(Captcha1.Text != UserTextBox.Text )
{
--Display error message--
}
else
{
--proceed to next--
}
Great Sir
ReplyDelete