How to create Captcha in ASP.NET
Posted
by
Samir R. Bhogayta
on Samir ASP.NET with C# Technology
See other posts from Samir ASP.NET with C# Technology
or by Samir R. Bhogayta
Published on 2013-04-10T00:28:00.002-07:00
Indexed on
2013/06/24
16:35 UTC
Read the original article
Hit count: 226
Filed under:
1. Create one page with name "Captcha.aspx"
2. No any control require in this page
3. Go to Captcha.aspx.vb write the below code
Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
'create object of Bitmap Class and set its width and height.
Dim objBMP As Bitmap = New Bitmap(180, 51)
'Create Graphics object and assign bitmap object to graphics' object.
Dim objGraphics As Graphics = Graphics.FromImage(objBMP)
objGraphics.Clear(Color.White)
objGraphics.TextRenderingHint = TextRenderingHint.AntiAlias
Dim objFont As Font = New Font("arial", 30, FontStyle.Bold)
'genetating random 6 digit random number
Dim randomStr As String = GeneratePassword()
'set this random number in session
Session.Add("randomStr", randomStr)
Session.Add("randomStrCountry", randomStr)
objGraphics.DrawString(randomStr, objFont, Brushes.Black, 2, 2)
Response.ContentType = "image/GIF"
objBMP.Save(Response.OutputStream, ImageFormat.Gif)
objFont.Dispose()
objGraphics.Dispose()
objBMP.Dispose()
End Sub
Public Function GeneratePassword() As String
' Below code describes how to create random numbers.some of the digits and letters
' are ommited because they look same like "i","o","1","0","I","O".
Dim allowedChars As String = "a,b,c,d,e,f,g,h,j,k,m,n,p,q,r,s,t,u,v,w,x,y,z,"
allowedChars += "A,B,C,D,E,F,G,H,J,K,L,M,N,P,Q,R,S,T,U,V,W,X,Y,Z,"
allowedChars += "2,3,4,5,6,7,8,9"
Dim sep() As Char = {","c}
Dim arr() As String = allowedChars.Split(sep)
Dim passwordString As String = ""
Dim temp As String
Dim rand As Random = New Random()
Dim i As Integer
For i = 0 To 5 - 1 Step i + 1
temp = arr(rand.Next(0, arr.Length))
passwordString += temp
Next
Return passwordString
End Function
4. Use this page in you aspx page like this
//
your textbox to insert code by user.
2. No any control require in this page
3. Go to Captcha.aspx.vb write the below code
Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
'create object of Bitmap Class and set its width and height.
Dim objBMP As Bitmap = New Bitmap(180, 51)
'Create Graphics object and assign bitmap object to graphics' object.
Dim objGraphics As Graphics = Graphics.FromImage(objBMP)
objGraphics.Clear(Color.White)
objGraphics.TextRenderingHint = TextRenderingHint.AntiAlias
Dim objFont As Font = New Font("arial", 30, FontStyle.Bold)
'genetating random 6 digit random number
Dim randomStr As String = GeneratePassword()
'set this random number in session
Session.Add("randomStr", randomStr)
Session.Add("randomStrCountry", randomStr)
objGraphics.DrawString(randomStr, objFont, Brushes.Black, 2, 2)
Response.ContentType = "image/GIF"
objBMP.Save(Response.OutputStream, ImageFormat.Gif)
objFont.Dispose()
objGraphics.Dispose()
objBMP.Dispose()
End Sub
Public Function GeneratePassword() As String
' Below code describes how to create random numbers.some of the digits and letters
' are ommited because they look same like "i","o","1","0","I","O".
Dim allowedChars As String = "a,b,c,d,e,f,g,h,j,k,m,n,p,q,r,s,t,u,v,w,x,y,z,"
allowedChars += "A,B,C,D,E,F,G,H,J,K,L,M,N,P,Q,R,S,T,U,V,W,X,Y,Z,"
allowedChars += "2,3,4,5,6,7,8,9"
Dim sep() As Char = {","c}
Dim arr() As String = allowedChars.Split(sep)
Dim passwordString As String = ""
Dim temp As String
Dim rand As Random = New Random()
Dim i As Integer
For i = 0 To 5 - 1 Step i + 1
temp = arr(rand.Next(0, arr.Length))
passwordString += temp
Next
Return passwordString
End Function
4. Use this page in you aspx page like this
//
your textbox to insert code by user.
© Samir ASP.NET with C# Technology or respective owner