Best Online Resource For Non-Resident Indians      SEARCH COOLNRI.COM
 HOME  FORUMS  PORTALS & REVIEWS  RECIPES  INDIAN RESTAURANTS  GROCERY  CLASSIFIEDS  MOVIES / TV  CHAT  NEWS  HOROSCOPE  PAINTINGS  CONTACT  LOGIN
Welcome Guest Search | Active Topics | Members | Log In | Register

Implement a simple captcha in C#.NET Step 2 Options
vimaljy
Posted: Saturday, October 24, 2009 11:53:14 PM
Rank: Member
Groups: Member

Joined: 9/26/2008
Posts: 3
[b]Create a page with name “Captcha.aspx”
Source:

Add the following code to HTML:
IMG height="30" alt="" src=BuildCaptcha.aspx width="80">
asp:TextBox runat="Server" ID="txtCaptcha">


Add a button, And In button click add the following

if (Page.IsValid && (txtCaptcha.Text.ToString() ==
Session["RandomStr"].ToString()))
{
Response.Write("Code verification Successful");
}
else
{
Response.Write( "Please enter info correctly");
}



Create a new file called “BuildCaptcha.aspx

Source:
1: Bitmap objBMP = new Bitmap(60, 20);

2: Graphics objGraphics = Graphics.FromImage(objBMP);

3: objGraphics.Clear(Color.Wheat);

4: objGraphics.TextRenderingHint = TextRenderingHint.AntiAlias;

5:

6:

7: //' Configure font to use for text

8: Font objFont = new Font("Arial", 8, FontStyle.Italic);

9: string randomStr = "";

10: char[] myArray = new char[5];

11: int x;

12:

13: //That is to create the random # and add it to our string

14: Random autoRand = new Random();

15: for (x = 0; x < 5; x++)

16: {

17: myArray[x] = System.Convert.ToChar(autoRand.Next(65,90));

18: randomStr += (myArray[x].ToString());

19: }

20:

21: //This is to add the string to session, to be compared later

22: Session.Add("RandomStr", randomStr);

23:

24: //' Write out the text

25: objGraphics.DrawString(randomStr, objFont, Brushes.Red, 3, 3);

26:

27: //' Set the content type and return the image

28: Response.ContentType = "image/GIF";

29: objBMP.Save(Response.OutputStream, ImageFormat.Gif);

30: objFont.Dispose();

31: objGraphics.Dispose();

32: objBMP.Dispose();

Now access captcha.aspx from browser
Users browsing this topic
Guest


Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Main Forum RSS : RSS

COOLNRI.COM 2006-2011 |  TERMS OF USE |  PRIVACY STATEMENT |  SEARCH 
ProjectGlide - Free Effective Project Management