This
article will show you how you can create
a captcha code in asp.net and perform validation weather user have entered correct
captcha or not.
Now for
this article fist we will create a new asp.net application and add two pages.
First page we will use to generate the captcha and second to display the
captcha and validation.
So first we will see the page which we will use to generate the captcha.
So first we will see the page which we will use to generate the captcha.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Captcha.aspx.cs" Inherits="Shoping_Cart.Captcha" %>
|
In this we will put only page tag and remove rest of the tags from the page. Now check the code to generate the Captcha.
using System;
using
System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.UI;
using
System.Web.UI.WebControls;
namespace Shoping_Cart
{
public partial class Captcha : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs 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();
}
Session["Captcha"] = strCaptcha;
ImageConverter
converter = new ImageConverter();
Response.BinaryWrite((byte[])converter.ConvertTo(CaptchaGeneration(strCaptcha), typeof(byte[])));
}
public Bitmap
CaptchaGeneration(string captchatxt)
{
Bitmap bmp = new Bitmap(133, 48);
using (Graphics graphics = Graphics.FromImage(bmp))
{
Font font = new Font("Tahoma", 14);
graphics.FillRectangle(new SolidBrush(Color.Gray), 0, 0, bmp.Width, bmp.Height);
graphics.DrawString(captchatxt, font, new SolidBrush(Color.Gold), 25, 10);
graphics.Flush();
font.Dispose();
graphics.Dispose();
}
return bmp;
}
}
}
|
In above
code I have generated the random number and character combination. And then I have
converted the string into image.
One of the
most important things just stores the captcha string into session. This will
help use while validation by entered captcha.
Now we will
see how to display the captcha and perform validation. For this if user enter
correct captcha he will be redirected to the success page.
<form id="form1" runat="server">
<div>
<asp:Image runat="server" ImageUrl="~/Captcha.aspx"></asp:Image><br />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Submit" OnClick="Button1_Click" />
<br />
<asp:Label ID="lblmessage"
runat="server" style="color: #FF3300" Text=""></asp:Label>
</div>
</form>
|
Now check
the validation code.
protected void Button1_Click(object sender, EventArgs e)
{
string captcha =
Session["Captcha"].ToString();
if (captcha == TextBox1.Text)
{
Response.Redirect("SuccessPage.aspx");
}
else
{
lblmessage.Text = "Please
enter a valid captcha.";
}
}
|
0 comments:
Please let me know your view