How to Make Capcha Code in Web Application

Hi ,

Here iam going to make a Capcha Code into our web application before that we have to know that is Capcha Code????

A CAPTCHA or Captcha is a type of challenge-response test used in computing to ensure that the response is not generated by a computer. The process usually involves one computer (a server) asking a user to complete a simple test which the computer is able to generate and grade. Because other computers are unable to solve the CAPTCHA, any user entering a correct solution is presumed to be human. Thus, it is sometimes described as a reverse Turing test, because it is administered by a machine and targeted to a human, in contrast to the standard Turing test that is typically administered by a human and targeted to a machine. A common type of CAPTCHA requires that the user type letters or digits from a distorted image that appears on the screen.


This is a Simple aspx page having a code to create a Cappcha Code

using System;
using System.Collections;
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;
using System.Drawing;
using System.Drawing.Text;
using System.Drawing.Imaging;


public partial class Capchapage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Bitmap objBMP = new System.Drawing.Bitmap(60, 20); // here we decide the size of capcha image
Graphics objGraphics = System.Drawing.Graphics.FromImage(objBMP);
objGraphics.Clear(Color.Gray);
objGraphics.TextRenderingHint = TextRenderingHint.AntiAlias;
//' Configure font to use for text
Font objFont = new Font("Arial", 8, FontStyle.Bold);
string randomStr = "";
int[] myIntArray = new int[5];
int x;
//That is to create the random # and add it to our string
Random autoRand = new Random();
for (x = 0; x <>
{
myIntArray[x] = System.Convert.ToInt32(autoRand.Next(0, 9));
randomStr += (myIntArray[x].ToString());
}
//This is to add the string to session cookie, to be compared later
Session.Add("randomStr", randomStr);
//' Write out the text
objGraphics.DrawString(randomStr, objFont, Brushes.White, 3, 3);
//' Set the content type and return the image
Response.ContentType = "image/GIF";
objBMP.Save(Response.OutputStream, ImageFormat.Gif);
objFont.Dispose();
objGraphics.Dispose();
objBMP.Dispose();

}
}


*****************************************************************************
After that you simple use this Capcha image into the web page like..

// img alt="" height="30" src="Chapcha.aspx" width="80"
// here we use image and text box to check capcha value

<%-- width="80" />

--%>
//
//

Now the Code on the .cs file on Button Click..

protected void btn_submit_Click(object sender, EventArgs e)
{
try
{
if (Session["randomStr"].ToString().Equals(txt_captcha.Text.Trim()))
{

int log = logic.Loginchk(txt_AccountNo.Text, txt_Password.Text);
if (log == 1)
{
//Logical Part
}
else
{
// Else Part for the logic
}
}
else
{
// lbl_capcha.Text = "Capcha Code is not Matched!!!";
}
}
catch (Exception ex)
{
}
}

0 comments: