Implement Anti-Robot Security Code in ASP.NET

With this trick you can quickly create a security code system for your website.

To create a security code we will follow the following steps:

We have to add in the form that we want to insert the security code the following controls:

  • An image (where your scr will be /AntiRobots.aspx)
  • A text type input, with an id= type= frm_Code_Security

In the VB code of the registration page we will declare a variable called bAntiBots

#Region “Members”

Dim bAntiBots As Boolean = False

#End Region

In the Page_Load:

If Not Is PostBack Then

Session(“AntiBots”) = GenerateRandomCode()

else

‘We check the antibots code

If frm_Security_Code.Value = Session(“AntiBots”).ToString() Then

bAntiBots = True

else

bAntiBots = False

Me.Session(“AntiBots”) = GenerateRandomCode()

End If

End If

We need to create the GenerateRandomCode function to generate a random security code

#Region “GenerateRandomCode”

Private Function GenerateRandomCode() As String

Dim random As New Random

Dim s As String = “”

Dim i As Int32 = 0

Dim iChr As Int32

While(i <; 6)
iChr = random.Next(48, 90)

While iChr >; 57 And iChr <; 65
iChr = random.Next(48, 90)

End While

s = String.Concat(s, Chr(iChr).ToString())

i = i + 1

End While

Returns

End Function

#End Region

To continue, we will need to create the class in charge of creating, manipulating and deforming the security code generated in the image.

Import System

Imports System.Drawing

Imports System.Drawing.Drawing2D

Imports System.Drawing.Imaging

Imports System.Drawing.Text

Public Class ImageAntiBots

Dim m_text As String

Dim m_width As Int32

Dim m_height As Int32

Dim m_familyName As String

Dim m_image As Bitmap

Dim m_random As New Random

#Region “Read Properties”

Public ReadOnly Property Text() As String

Get

Return m_text

End Get

End Property

See also  System variables in PHP

Public ReadOnly Property Width() As Int32

Get

m_width

End Get

End Property

Public ReadOnly Property Height() As Int32

Get

m_height

End Get

End Property

Public ReadOnly Property Image() As Bitmap

Get

Return m_image

End Get

End Property

#End Region

Public Sub New(ByVal s As String, ByVal width As Int32, ByVal height As Int32)

me.m_text = yes

Me.SetDimensions(width, height)

Me.GenerateImage()

End Sub

Public Sub New(ByVal s As String, ByVal width As Int32, ByVal height As Int32, ByVal familyName As String)

me.m_text = yes

Me.SetDimensions(width, height)

Me.SetFamilyName(familyName)

Me.GenerateImage()

End Sub

Private Sub SetDimensions(ByVal width As Int32, ByVal height As Int32)

If width <;= 0 Then
Throw New ArgumentOutOfRangeException(“width”, width, “Argument out of range, must be greater than zero.”)

End If

If (height <;= 0) Then
Throw New ArgumentOutOfRangeException(“height”, height, “Argument out of range, must be greater than zero.”)

End If

me.m_width = width

me.m_height = height

End Sub

Private Sub SetFamilyName(ByVal familyName As String)

Try

Dim font As Font = New Font(familyName, 12.0F)

me.m_familyName = familyName

font.Dispose()

Catch ex As Exception

Me.m_familyName = System.Drawing.FontFamily.GenericSerif.Name

end try

End Sub

Private Sub GenerateImage()

Dim bitmap As New Bitmap(Me.m_width, Me.m_height, PixelFormat.Format32bppArgb)

‘Create graphic object

Dim g As Graphics = Graphics.FromImage(bitmap)

g.SmoothingMode = SmoothingMode.AntiAlias

Dim rect As New RectangleF(0, 0, Me.m_width, Me.m_height)

‘Fill the background

Dim hatchBrush As New HatchBrush(HatchStyle.SmallConfetti, Color.LightGray, Color.White)

g.FillRectangle(hatchBrush, rect)

‘Set the source

Dim size As SizeF

Dim fontSize As Single = rect.Height + 1

Dim font As Font

‘Set the font size

Do

fontSize = fontSize – 1

font = NewFont(Me.m_familyName, fontSize, FontStyle.Bold)

size = g.MeasureString(Me.m_text, font)

Loop While(size.Width >; rect.Width)

‘Set the text format

Dim format As New StringFormat

format.Alignment = StringAlignment.Center

format.LineAlignment = StringAlignment.Center

See also  How do I install recaptcha v2 in PHP

Dim path As New GraphicsPath

path.AddString(Me.m_text, font.FontFamily, CType(font.Style, Int32), font.Size, rect, format)

Dim v As Single = 4.0F

Dim points As System.Drawing.PointF() = { _

New System.Drawing.PointF(m_random.Next(CType(rect.Width, Integer)) / v, m_random.Next(CType(rect.Height, Integer)) / v), _

New System.Drawing.PointF(rect.Width – m_random.Next(CType(rect.Width, Integer)) / v, m_random.Next(CType(rect.Height, Integer)) / v), _

New System.Drawing.PointF(m_random.Next(CType(rect.Width, Integer)) / v, rect.Height – m_random.Next(CType(rect.Height, Integer)) / v), _

New System.Drawing.PointF(rect.Width – m_random.Next(CType(rect.Width, Integer)) / v, rect.Height – m_random.Next(CType(rect.Height, Integer)) / v)}

Dim matrix As New Matrix

matrix.Translate(1, 3)

‘We distort the image

path.Warp(points, rect, matrix, 0)

‘draw the text

hatchBrush = New HatchBrush(HatchStyle.OutlinedDiamond, Color.Orange, Color.BlueViolet)

g.FillPath(hatchBrush, path)

‘Add effects

Dim m As Int32 = Math.Max(CType(rect.Width, Integer), CType(rect.Height, Integer))

Dim i As Int32 = 0

While i <; CType((rect.Width * rect.Height / 30.0F), Int32)
Dim x As Int32 = m_random.Next(CType(rect.Width, Integer))

Dim and As Int32 = m_random.Next(CType(rect.Height, Integer))

Dim w As Int32 = m_random.Next(CType(m / 50, Integer))

Dim h As Int32 = m_random.Next(CType(m / 50, Integer))

g.FillEllipse(hatchBrush, x, y, w, h)

i = i + 1

End While

font.Dispose()

hatchBrush.Dispose()

g.Dispose()

‘set the image

me.m_image = bitmap

End Sub

end class

Now in the project we will create the page that points to our image (the first control that we have added in the project, and in the Page_Load we will put this code:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

‘We create an image by creating the text saved in the session

Dim ab As New ImageAntiBots(Session(“AntiBots”).ToString(), 200, 50, “Arial”)

‘We change the response to the client to type “image/jpeg”

Me.Response.Clear()

Me.Response.ContentType = “image/jpeg”

‘Save the image in the Response

ab.Image.Save(Me.Response.OutputStream, ImageFormat.Jpeg)

End Sub

And finally, in the registration form, where we have added the controls, in the submit button, use this condition:

See also  Javascript SWITCH structure

If bAntiBots Then

‘ Create the record

else

‘ The security code is not set correctly

end if

You can download all the source code by clicking

Loading Facebook Comments ...
Loading Disqus Comments ...