RSS

CAPTCHA Implementation in ASP.NET

07 Apr

I want to implement Captcha in my ASP.NET project

“Default.aspx

<label>Image verification text (case sensitive/text only):</label>

<asp:TextBox ID=”txtImageVerification” runat=”server” ></asp:TextBox>

<asp:Image ID=”imgVerification” runat=”server” ImageAlign=”absMiddle” />

<br />

<div style=”padding-left: 170px;”>

<asp:Button ID=”btnsubmit” ValidationGroup=”grpContactUs” runat=”server” Text=”Submit”  >

<asp:validationsummary Visible=”True” id=”valSummary” runat=”server” howSummary=”False” DisplayMode=”BulletList” ShowMessageBox=”True” HeaderText=”The following errors were found:” ValidationGroup=”grpContactUs” />

</div>

 

 

CodeBehind Side

 

“Default.aspx.vb

Imports siteCaptcha

Partial Class _Default

Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

If Not Page.IsPostBack Then

Dim rnd As New Random

Session(“ImageValidate”) = validateImage.GenerateVCodeImage(Server.MapPath(“images/imageverfication/ImgVerify.jpeg”))

imgVerification.ImageUrl = “~/images/imageverfication/ImgVerify.jpeg?id=” & rnd.Next(100).ToString

End If

End Sub

 

Protected Sub btnsubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnsubmit.Click

Try

If Not Session(“ImageValidate”) Is Nothing Then

If Session(“ImageValidate”).ToString <> txtImageVerification.Text Then

Dim rnd As New Random

Session(“ImageValidate”) = validateImage.GenerateVCodeImage(Server.MapPath(“images/imageverfication/ImgVerify.jpeg”))

imgVerification.ImageUrl = “~/images/imageverfication/ImgVerify.jpeg?id=” & rnd.Next(100).ToString

Me.PMsg.InnerHtml = “Image verification text did not match !”

Me.PMsg.Visible = True

Exit Sub

Else

Me.PMsg.InnerHtml = “SUCESSFULLY VERIFICATION OF IMAGE”

Me.PMsg.Visible = True

 

End If

 

Else

Dim rnd As New Random

Session(“ImageValidate”) = validateImage.GenerateVCodeImage(Server.MapPath(“images/imageverfication/ImgVerify.jpeg”))

imgVerification.ImageUrl = “~/images/imageverfication/ImgVerify.jpeg?id=” & rnd.Next(100).ToString

Me.PMsg.InnerHtml = “Image verification text did not match !”

Me.PMsg.Visible = True

Exit Sub

End If

Catch exs As System.Threading.ThreadAbortException

Response.Redirect(Request.UrlReferrer.ToString)

Catch ex As Exception

Throw ex

End Try

End Sub

End Class

 

Create images folder on root then subfolder is imageverfication. See below image

 

 


Create  validateImage.vb in App_Code

Imports System.Drawing.Drawing2D

Imports System.Drawing.Imaging

Imports System.Drawing.Text

Namespace siteCaptcha

Public Class validateImage

Private Shared 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

Private Shared 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

Public Shared Function GenerateVCodeImage(ByVal path As String) As String

Dim oBitmap As Drawing.Bitmap = New Drawing.Bitmap(130, 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, 140, 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)

sText = sText.ToUpper

Dim tmpStr As String = sText

Dim chr As Char() = sText.ToCharArray

sText = “”

For chrCount As Integer = 0 To chr.Length – 1

sText &= chr(chrCount) & ” ”

Next

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

oBitmap.Save(path, ImageFormat.Jpeg)

oBitmap.Dispose()

‘sText = sText.ToLower

Return tmpStr

End Function

 

End Class

 

End Namespace

 

 

Advertisements
 
2 Comments

Posted by on April 7, 2011 in ASP Dot Net C#

 

2 responses to “CAPTCHA Implementation in ASP.NET

  1. pinoyrichjerk.com

    April 30, 2013 at 11:20 pm

    Good day! This is kind of off topic but I
    need some advice from an established blog. Is
    it hard to set up your own blog? I’m not very techincal but I can figure things out pretty fast. I’m thinking
    about setting up my own but I’m not sure where to start. Do you have any points or suggestions? Appreciate it

     
  2. Electronic Cigarette Side Effects

    June 1, 2013 at 12:29 pm

    If you wish for to grow your experience just
    keep visiting this site and be updated with
    the hottest gossip posted here.

     

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: