Captcha implementation Using ASP.NET

Manimaddu
Posted by Manimaddu under ASP.NET category on | Points: 40 | Views : 538
Hi All...

Here i want implement Captcha using ASP.NET

ShowCaptcha.aspx:


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ShowCapcha.aspx.cs" Inherits="CapchaDemo1.ShowCapcha" %>

<!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></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td>
<asp:Image ID="imgCaptcha" runat="server" ImageUrl="~/CreateCaptcha.aspx?New=1" />
</td>
</tr>
<tr>
<td>
<asp:TextBox ID="txtCaptcha" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="lblMessage" runat="server"></asp:Label>
</td>
</tr>
<tr>
<td>
<asp:Button ID="btnCaptcha" runat="server" Text="Validate Cpatcha Code" OnClick="btnCaptcha_Click" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>


ShowCaptcha.aspx.cs:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;

namespace CapchaDemo1
{
public partial class ShowCapcha : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}

protected void btnCaptcha_Click(object sender, EventArgs e)
{
if (Session["CaptchaCode"] != null && txtCaptcha.Text == Session["CaptchaCode"].ToString())
{
lblMessage.ForeColor = Color.Green;
lblMessage.Text = "Captcha code validated successfully!!";
}
else
{
lblMessage.ForeColor = Color.Red;
lblMessage.Text = "Captcha code is wrong!!";
}
}
}
}


CreateCaptcha.aspx:


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CreateCaptcha.aspx.cs"
Inherits="CapchaDemo1.CreateCaptcha" %>

<!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></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>


CreateCaptcha.aspx.cs:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Text;


namespace CapchaDemo1
{
public partial class CreateCaptcha : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
CreateCaptchaImage();
}

private void CreateCaptchaImage()
{
string code = GetRandomText();
Bitmap bitmap = new Bitmap(200, 60, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
Graphics g = Graphics.FromImage(bitmap);
Pen pen = new Pen(Color.Yellow);
Rectangle rect = new Rectangle(0, 0, 200, 60);
SolidBrush blue = new SolidBrush(Color.CornflowerBlue);
SolidBrush black = new SolidBrush(Color.Black);
int counter = 0;
g.DrawRectangle(pen, rect);
g.FillRectangle(blue, rect);
Random rand = new Random();
for (int i = 0; i < code.Length; i++)
{
g.DrawString(code[i].ToString(), new Font("Tahoma", 10 + rand.Next(15, 20), FontStyle.Italic), black, new PointF(10 + counter, 10));
counter += 28;
}
DrawRandomLines(g);
bitmap.Save(Response.OutputStream, ImageFormat.Gif);
g.Dispose();
bitmap.Dispose();
}

/// Method for drawing lines

private void DrawRandomLines(Graphics g)
{
SolidBrush yellow = new SolidBrush(Color.Yellow);
for (int i = 0; i < 20; i++)
{ g.DrawLines(new Pen(yellow, 1), GetRandomPoints()); }

}

/// method for gettting random point position
private Point[] GetRandomPoints()
{
Random rand = new Random();
Point[] points = { new Point(rand.Next(0, 150), rand.Next(1, 150)), new Point(rand.Next(0, 200), rand.Next(1, 190)) };
return points;
}
/// Method for generating random text of 5 cahrecters as captcha code
private string GetRandomText()
{
StringBuilder randomText = new StringBuilder();
string alphabets = "012345679ACEFGHKLMNPRSWXZabcdefghijkhlmnopqrstuvwxyz";
Random r = new Random();
for (int j = 0; j <= 5; j++)
{ randomText.Append(alphabets[r.Next(alphabets.Length)]); }
Session["CaptchaCode"] = randomText.ToString();
return Session["CaptchaCode"] as String;
}
}
}


Thank You..

Comments or Responses

Login to post response