What is Captcha code:-
Captcha code is a one kind of program that Generate Grade and Test the user or Human is Pass or Not.In this Example we explain that how to create a captcha code in asp.net to increase our website Security.
Captcha code is nothing but it is a one kind image in which Text,Letter,number,Special Symbol are Display as in Define Format and are generally used in every website for security purpose.
when image is Generated by Fuction it will assign to Image Control and Display this Image. user have enter Text into TextBox same as Display in Image. after Click on submit Button it will check the Captcha code same as entered by user if true then proceed further otherwise it will create a new Captch Code if user enter code is False.
Generally Captch Code is Generated are stored in Session Variable and assign this image to Image Control.
Advantages Of the Captha Code :-
1. Protecting Website Registration:-
Means when user create a Registration in your Website at that time Captcha Code is Generated and user have to insert it to register into a website so Dummy user or robort can not Access your Website.
2. Online Polls
3. Login into an Bank account.
4. Sending Email etc…
How to Send SMS to User in asp.net Send SMS in asp.net
Upload File and Bind to Gridview Upload File in Database and Bind to Gridview
Example of AutoComplete TextBox Autocomplete TextBox in Ajax
CreateCaptcha.aspx:-
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CreateCaptcha.aspx.cs" Inherits="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 id="Head1" runat="server">
<title>Untitled
Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
CreateCaptcha.aspx.cs:-
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.Imaging;
using System.Drawing;
using System.Text;
public partial class CreateCaptcha : System.Web.UI.Page
{
Random rand = new Random();
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
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.Honeydew);
SolidBrush black = new SolidBrush(Color.Green);
int counter = 0;
g.DrawRectangle(pen, rect);
g.FillRectangle(blue, rect);
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();
}
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()); }
}
private Point[] GetRandomPoints()
{
Point[] points = { new Point(rand.Next(0, 150), rand.Next(1, 150)), new Point(rand.Next(0, 200), rand.Next(1,
190)) };
return points;
}
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();
string q = randomText.ToString();
return Session["CaptchaCode"] as String;
}
ShowCaptcha.aspx:-
<%@ Page Language="C#" AutoEventWireup="true" CodeFile
="ShowCaptcha.aspx.cs" Inherits="Captcha_Code" %>
<!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>Untitled Page</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;
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;
public partial class Captcha_Code : 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!!";
}
}
}
}
0 comments:
Post a Comment