You are on page 1of 21

The below example will generate a captcha code with numbers and alphabets (small and

capital letters). You can change the variable combination value according to your
requirement.
Output of the below example:

Create two pages GenerateCaptcha.aspx and Registration.aspx


GenerateCaptcha.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFil


e="GenerateCaptcha.aspx.cs" Inherits="GenerateCaptcha"%>

<!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>

GenerateCaptcha.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;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;

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();
}
}

Registration.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Registration.aspx.cs"


Inherits="Registration" %>

<!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>
<asp:ScriptManager ID="SM1" runat="server">
</asp:ScriptManager>
<table style="border: solid 1px black; padding: 20px; position: relative; top:
50px;"
align="center">
<tr>
<td>
Email ID :
</td>
<td>
<asp:TextBox ID="txtEmailID" runat="server" Width="200px"></asp:Text

Box>

</td>
</tr>
<tr>
<td>
Password :
</td>
<td>
<asp:TextBox ID="txtPassword" runat="server" TextMode="Password" Wi
dth="200px"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Confirm Password :

</td>
<td>
<asp:TextBox ID="txtConfirmPassword" runat="server" TextMode="Passw
ord" Width="200px"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Enter Below Code :
</td>
<td>
<asp:TextBox ID="txtCaptcha" runat="server" Width="200px"></asp:Tex
tBox>
</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="bt
nRegister_Click"/>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>

Registration.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.Text;

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
= "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
StringBuilder captcha = new StringBuilder();
for (int i = 0; i < 6; 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 btnRegister_Click(object sender, EventArgs e)


{
if (Session["captcha"].ToString() != txtCaptcha.Text)
Response.Write("Invalid Captcha Code");
else
Response.Write("Valid Captcha Code");
FillCapctha();
}

Captcha with dll file

There are some steps to Add Captcha image in Asp.NET Application. You
can download whole application from below link.You can easily add this captcha
image to your Asp.Net Application Without facing any problem. You can add
another captcha code also in any asp.net application from here.Both captcha
codes will be provided full security to your asp.net application. Please follow these
steps one by one which are given below.

Step1:- First create ASP.NET page (Default.aspx) in visual studio 2010-->


Now click the below link to download the captcha.dll file.
see captcha.dll file:-

Download
Step2:-Now Open Solution Explorer -->Add new Folder(Bin)->open
the captcha.dll file->copy all the file -> Now paste it in the Bin Folder.
see it:-

Step3:- Now Write the code for captcha control in Source file as
shown below:
see it:-

Step4:-

Now Drag and Drop Text-box,Button, Label and Validation


controls on Default.aspx page from theToolbox.which is as shown below:see it:-

Step5:- Now Double click on Button control(Verify) and write the


following code.
01

using System;

02

using System.Collections.Generic;

03

using System.Linq;

04

using System.Web;

05

using System.Web.UI;

06

using System.Web.UI.WebControls;

07
08 public partial class _Default : System.Web.UI.Page
09 {
10
protected void Page_Load(object sender, EventArgs e)
11
{
12
13
}
14
protected void Button1_Click(object sender, EventArgs e)
15
{
16
captcha1.ValidateCaptcha(TextBox2.Text.Trim());
17
if (captcha1.UserValidated)
18
{
19
Label1.ForeColor = System.Drawing.Color.Green;
Label1.Text = "You have Entered Valid Captcha
20
Characters";
21
}
22
else
23
{
24
Label1.ForeColor = System.Drawing.Color.Red;
Label1.Text = "You have Entered InValid Captcha
25
Characters please Enter again";
26
}
27
}
28
}

see it:-

Step6:- Now open the web.config file and write the following codes as
shown below.
see it:-

Step7:- Run the Application (press F5).


see it:
OUTPUT:

when we Enter the correct captcha image In the Text-Box and click Verify
Button,then we see following output:-

when we Enter the Incorrect captcha image In the Text-Box and Click
Verify Button,then we see following output :

Step8:-If you want to run this whole web application directly on your
visual studio then follow below steps which are given below:
First Download this whole application from bottom and Extract it-->Copy
this whole file (cltr+c).

Now open your visual studio --> File --.New --.website -->Select ASP.NET
Empty website -->OK -->Now open Solution Explorer -->Right click on
project -->Paste this whole application which you have copied previously.

Now Run the application-->then you can run this application successfully.

If you are facing problem-->Download captcha.dll file from step 1 -> Extract it and copy all dll files -->open solution explorer -->Past this dll
file in bin Folder.

Now again run (press F5) then it will definitely work.


Please share this application with your friends so that it may helpful to others
also.If any problem ask frequently.I will solve your doubt definitely.

Password strength
The PasswordStrengthExtender Control targets a TextBox control that is used for
entering Passwords.When user starts typing the text in text box ,this control
calculates the strength of the text using different specified properties.Here i have
set different properties such as TargetControlID, DisplayPosition ,
PrefixText,StrengthIndicatorType, MinimumLowerCaseCharacters,
MinimumNumericCharacters, MinimumSymbolCharacters,

PreferredPasswordLength, TextStrengthDescriptions, StrengthStyles.You can see


it in source file.
First,You have to install Ajax Toolkit in your visual studio.
There are some steps to implement this whole concepts in asp.net website as
given below:Step 1: First open your visual studio -->File-->New -->Web Site-->
Select ASP.NET Empty website-->OK-->Open Solution Explorer window->Add a New Web form (Default.aspx)--> drag and
dropTookitScriptManager, text box and button controls as shown below:-

Step 2: Now open Source file-->Drag and drop PasswordStrength control


and set its properties as given below:01

<%@ Page Language="C#" AutoEventWireup="true"


CodeFile="Default.aspx.cs" Inherits="_Default" %>

02
03

<%@ Register assembly="AjaxControlToolkit"


namespace="AjaxControlToolkit" tagprefix="asp" %>

04
05

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"


"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

06
07

<html xmlns="http://www.w3.org/1999/xhtml">

08
09
10
11
12
13
14

<head runat="server">
<title>Welcome to MY.NET Tutorials</title>
</head>
<body>
<form id="form1" runat="server">
<div>

<span class="style1"><strong>How to implement Password


Strenghth control on
16
asp.net website
17
18
</strong>
15

<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat=


"server">
20
</asp:ToolkitScriptManager>
21
</span>
22
Enter
23
UserName<asp:TextBox ID="TextBox1" runat="server" Height="30px"
24
Width="200px"></asp:TextBox>
25
19

26
27
28
29
30

Enter Password
<asp:TextBox ID="TextBox2" runat="server" Height="30px"
ox>

TextMode="Password" Text= "*" Width="200px"></asp:TextB

31
32
3
<asp:PasswordStrength ID="PasswordStrength1" runat="server"
3 TargetControlID="TextBox2"
34

DisplayPosition="RightSide" PrefixText="Your Password


Strength is:"StrengthIndicatorType ="Text"

MinimumLowerCaseCharacters="1" MinimumNumericCharacters="1"
3
MinimumSymbolCharacters="1"
MinimumUpperCaseCharacters="1"Preferred
5
PasswordLength="10"
3
TextStrengthDescriptions="Poor;Average;Strength"StrengthSty
6 les="strengthpoor;strengthAverage;strengthUnBreakable">
37
</asp:PasswordStrength>
38
39
40

<asp:Button ID="Button1" runat="server" Height="37px"


style="font-weight: 700; background-color:
41
#6699FF" Text="Logon"
42
Width="108px" />
43
</div>
44
</form>
45
</body>
46
</html>

Step 3: Now Run the Application (press F5)--> You will see following output as
shown below:-

How to add ajax toolkit in to our website


Step 1:- First Download Ajax Toolkit from official website here as shown below:-

Step 2:- Now if downloaded the Ajax Toolkit-->First create a new folder on
Desktop or other place -->Put your Ajax Toolkit(compress file) in this folder
-->after that Extract this Ajax Toolkit-->after that
ExtractAjaxControlToolkitSampleSite as shown below:-

Step 3:- Now open your visual studio -->File -->New -->Website-->ASP.NET
Empty Website -->Add a New Web Form From Solution Explorer (Default.aspx)
--> Now open Toolbox -->Right click in Tool Box --> Add Tab -->Write your Tab
Name as Ajax Toolkit as shown below:-

Step 4:- Now Right click on Ajax Toolkit -->Choose Items->Select Browse Button -->Go Ajax Toolkit Folder-->Open Bin folder in your
Ajax Toolkit -->Select AjaxControlToolkit.dll file as shown below --> Press Open
-->Now you have done-->Now you will see ,ajax toolkit installed in your Toolbox.

Step 5:- Now Drag and Drop TextBox and ToolkitScriptManager controls on the
Form (default.aspx) as shown below:-

Step 6:- Now open Source Mode of default.aspx page --> and Write the
following codes for Calendar Extender Control -->
Set TargetControlID="TextBox1" as given below:1

<asp:CalendarExtender
ID="CalendarExtender1" TargetControlID="TextBox1"runat="server" />

See Default.aspx source codes:-

Step 7:- Now Run the Application (Press F5)-->Click TextBox control and select
the date from Calendar as shown below:-

Note:- In our coming tutorial, i will explain each Ajax Toolkit's controls with real
life example.

You might also like