In this blog,We will see how to make a Registration Page using CAPTCHA in C#.
For this,We create Registration page and GenerateCaptcha page.
In GenerateCaptcha page, we write code for genrating captcha.
In Registration page,we create textboxes for Name,Email,Password,captcha,
buttons for Refresh and Registration
and one updatepanel and Image for captcha.
On page load event of Registration page,we create text for captcha using Random number
and stringbuilder,store this to Session["captcha"] and call GenerateCaptcha page to make colourful captcha using Bitmap,Graphics and RectangleF.
When user click on Registration button after filling name,email,password,captcha
if value of captcha stored in Session["captcha"] and text of txtCaptcha textbox matches,
user will successfully registered
if both value not matches,user will get message of invalid captcha.
Step 1)We create Registration page like below.
Step 2) Code of Registration.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Registration.aspx.cs" Inherits="Registration" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server" >
<div>
<asp:ScriptManager ID="SM1" runat="server"></asp:ScriptManager>
<table style="border: solid 1px black; padding: 20px; position: relative; top: 50px; background-color: #CC3300;"
align="center" >
<tr>
<td>
Name:
</td>
<td>
<asp:TextBox ID="TextBox1" runat="server" Width="200px"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Email ID :
</td>
<td>
<asp:TextBox ID="txtEmailID" runat="server" Width="200px"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Password :
</td>
<td>
<asp:TextBox ID="txtPassword" runat="server" TextMode="Password" Width="200px"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Confirm Password :
</td>
<td>
<asp:TextBox ID="txtConfirmPassword" runat="server" TextMode="Password" Width="200px"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Enter Below Code :
</td>
<td>
<asp:TextBox ID="txtCaptcha" runat="server" Width="200px"></asp:TextBox>
</td>
</tr>
<tr>
<td>
</td>
<td valign="middle">
<asp:UpdatePanel ID="UP1" runat="server">
<ContentTemplate>
<table>
<tr>
<td style="height: 50px; width:100px;">
<asp:Image ID="imgCaptcha" runat="server" />
</td>
<td valign="middle">
<asp:Button ID="btnRefresh" runat="server" Text="Refresh" OnClick="btnRefresh_Click" />
</td>
</tr>
</table>
</ContentTemplate>
</asp:UpdatePanel>
</td>
</tr>
<tr>
<td colspan="2" align="center">
<asp:Button ID="btnRegiser" runat="server" Text="Register" OnClick="btnRegiser_Click"/>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
Step 3) Code of GenerateCaptcha.aspx.cs
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class GenerateCaptcha : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.Clear();
int height = 30;
int width = 100;
Bitmap bmp = new Bitmap(width, height);
RectangleF rectf = new RectangleF(10, 5, 0, 0);
Graphics g = Graphics.FromImage(bmp);
g.Clear(Color.White);
g.SmoothingMode = SmoothingMode.AntiAlias;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
g.DrawString(Session["captcha"].ToString(), new Font("Thaoma", 12, FontStyle.Italic), Brushes.Green, rectf);
g.DrawRectangle(new Pen(Color.Red), 1, 1, width - 2, height - 2);
g.Flush();
Response.ContentType = "image/jpeg";
bmp.Save(Response.OutputStream, ImageFormat.Jpeg);
g.Dispose();
bmp.Dispose();
}
}
Step 4)Code of Registration.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Registration : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
FillCapctha();
}
}
void FillCapctha()
{
try
{
Random random = new Random();
string combination = "0123456789123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz@*#";
StringBuilder captcha = new StringBuilder();
for (int i = 0; i < 7; i++)
captcha.Append(combination[random.Next(combination.Length)]);
Session["captcha"] = captcha.ToString();
imgCaptcha.ImageUrl = "GenerateCaptcha.aspx?" + DateTime.Now.Ticks.ToString();
}
catch
{
throw;
}
}
protected void btnRefresh_Click(object sender, EventArgs e)
{
FillCapctha();
}
protected void btnRegiser_Click(object sender, EventArgs e)
{
if (Session["captcha"].ToString() == txtCaptcha.Text)
Response.Write("Registration successful");
else
Response.Write("invalid Captcha Code");
FillCapctha();
}
}
Step 5)
if value of captcha stored in Session["captcha"] and text of txtCaptcha textbox matches,
user will successfully registered
Step 6)if both value not matches,user will get message of invalid captcha.
you also like this blog.For this,We create Registration page and GenerateCaptcha page.
In GenerateCaptcha page, we write code for genrating captcha.
In Registration page,we create textboxes for Name,Email,Password,captcha,
buttons for Refresh and Registration
and one updatepanel and Image for captcha.
On page load event of Registration page,we create text for captcha using Random number
and stringbuilder,store this to Session["captcha"] and call GenerateCaptcha page to make colourful captcha using Bitmap,Graphics and RectangleF.
When user click on Registration button after filling name,email,password,captcha
if value of captcha stored in Session["captcha"] and text of txtCaptcha textbox matches,
user will successfully registered
if both value not matches,user will get message of invalid captcha.
Step 1)We create Registration page like below.
Step 2) Code of Registration.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Registration.aspx.cs" Inherits="Registration" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server" >
<div>
<asp:ScriptManager ID="SM1" runat="server"></asp:ScriptManager>
<table style="border: solid 1px black; padding: 20px; position: relative; top: 50px; background-color: #CC3300;"
align="center" >
<tr>
<td>
Name:
</td>
<td>
<asp:TextBox ID="TextBox1" runat="server" Width="200px"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Email ID :
</td>
<td>
<asp:TextBox ID="txtEmailID" runat="server" Width="200px"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Password :
</td>
<td>
<asp:TextBox ID="txtPassword" runat="server" TextMode="Password" Width="200px"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Confirm Password :
</td>
<td>
<asp:TextBox ID="txtConfirmPassword" runat="server" TextMode="Password" Width="200px"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Enter Below Code :
</td>
<td>
<asp:TextBox ID="txtCaptcha" runat="server" Width="200px"></asp:TextBox>
</td>
</tr>
<tr>
<td>
</td>
<td valign="middle">
<asp:UpdatePanel ID="UP1" runat="server">
<ContentTemplate>
<table>
<tr>
<td style="height: 50px; width:100px;">
<asp:Image ID="imgCaptcha" runat="server" />
</td>
<td valign="middle">
<asp:Button ID="btnRefresh" runat="server" Text="Refresh" OnClick="btnRefresh_Click" />
</td>
</tr>
</table>
</ContentTemplate>
</asp:UpdatePanel>
</td>
</tr>
<tr>
<td colspan="2" align="center">
<asp:Button ID="btnRegiser" runat="server" Text="Register" OnClick="btnRegiser_Click"/>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
Step 3) Code of GenerateCaptcha.aspx.cs
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class GenerateCaptcha : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.Clear();
int height = 30;
int width = 100;
Bitmap bmp = new Bitmap(width, height);
RectangleF rectf = new RectangleF(10, 5, 0, 0);
Graphics g = Graphics.FromImage(bmp);
g.Clear(Color.White);
g.SmoothingMode = SmoothingMode.AntiAlias;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
g.DrawString(Session["captcha"].ToString(), new Font("Thaoma", 12, FontStyle.Italic), Brushes.Green, rectf);
g.DrawRectangle(new Pen(Color.Red), 1, 1, width - 2, height - 2);
g.Flush();
Response.ContentType = "image/jpeg";
bmp.Save(Response.OutputStream, ImageFormat.Jpeg);
g.Dispose();
bmp.Dispose();
}
}
Step 4)Code of Registration.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Registration : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
FillCapctha();
}
}
void FillCapctha()
{
try
{
Random random = new Random();
string combination = "0123456789123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz@*#";
StringBuilder captcha = new StringBuilder();
for (int i = 0; i < 7; i++)
captcha.Append(combination[random.Next(combination.Length)]);
Session["captcha"] = captcha.ToString();
imgCaptcha.ImageUrl = "GenerateCaptcha.aspx?" + DateTime.Now.Ticks.ToString();
}
catch
{
throw;
}
}
protected void btnRefresh_Click(object sender, EventArgs e)
{
FillCapctha();
}
protected void btnRegiser_Click(object sender, EventArgs e)
{
if (Session["captcha"].ToString() == txtCaptcha.Text)
Response.Write("Registration successful");
else
Response.Write("invalid Captcha Code");
FillCapctha();
}
}
Step 5)
if value of captcha stored in Session["captcha"] and text of txtCaptcha textbox matches,
user will successfully registered
Step 6)if both value not matches,user will get message of invalid captcha.
Abstraction and Encapsulation in OOPS
Inheritance in OOPS
No comments :
Post a Comment