DotNet Tutorials

Server Intellect

 Verification code to prevent auto signup in ASP.NET(VB)

By entering the verification code shown on the web page when a user sign up, the web site or application can prevent automated registrations. This reduces system loads and ensures better performance and security of web site or application. This tutorial will show you how to create the randomly generated verification code in ASP.NET 2.0 and VB. In order to create a random verification code, we create a method called CreateRandomCode at first
Private Function generateVCode(ByVal CodeLength As Integer) As String
Dim VCode As String = String.Empty
Dim randObj As New Random()
Dim c As Integer = 63
For i As Byte = 1 To CodeLength
c = randObj.Next(35)
If c >= 10 Then
c += 7
End If
c += 48
VCode += Chr(c)
Next
Return VCode
End Function

If you're looking for a really good web host, try Server Intellect - we found the setup procedure and control panel, very easy to adapt to and their IT team is awesome!

Then we generate verification code character string as exported image

Private Function generateHatchStyle() As HatchStyle
Dim slist As New ArrayList
For Each style As HatchStyle In System.Enum.GetValues(GetType(HatchStyle))
slist.Add(style)
Next

Dim randObj As New Random()
Dim index As Integer = randObj.Next(slist.Count - 1)

Return CType(slist(index), HatchStyle)
End Function

Use a method GenerateVCodeImage to print image

Private Function GenerateVCodeImage()
Dim oBitmap As Drawing.Bitmap = New Drawing.Bitmap(90, 35)
Dim oGraphic As Drawing.Graphics = Drawing.Graphics.FromImage(oBitmap)
Dim foreColor As System.Drawing.Color
Dim backColor As System.Drawing.Color

Dim sText As String = generateVCode(6)
Dim sFont As String = "Comic Sans MS"

foreColor = System.Drawing.Color.FromArgb(220, 220, 220)
backColor = System.Drawing.Color.FromArgb(190, 190, 190)

Dim oBrush As New Drawing.Drawing2D.HatchBrush(CType(generateHatchStyle(), Drawing.Drawing2D.HatchStyle), foreColor, backColor)

Dim oBrushWrite As New Drawing.SolidBrush( Drawing.Color.Red)

oGraphic.FillRectangle(oBrush, 0, 0, 100, 50)
oGraphic.TextRenderingHint = Drawing.Text.TextRenderingHint.AntiAlias

Dim oFont As New Drawing.Font(sFont, 14)
Dim oPoint As New Drawing.PointF(5.0F, 4.0F)

oGraphic.DrawString(sText, oFont, oBrushWrite, oPoint)

Response.ContentType = "image/jpeg"
oBitmap.Save(Response.OutputStream, Drawing.Imaging.ImageFormat.Jpeg)
oBitmap.Dispose()

Return sText
End Function

Transfer function on Page_load

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim code As String = GenerateVCodeImage()
Session("VCode") = code
End Sub

We migrated our web sites to Server Intellect over one weekend and the setup was so smooth that we were up and running right away. They assisted us with everything we needed to do for all of our applications. With Server Intellect's help, we were able to avoid any headaches!



Looking for the C#.NET 2005 Version? Click Here!

Looking for more ASP.NET Tutorials? Click Here!

Download Project Source - Enter your Email to be emailed a link to download the Full Source Project used in this Tutorial!



100% SPAM FREE! We will never sell or rent your email address!
 
123 ASP

411 ASP

Dot Net Freaks

Server Intellect