0
Reply

Help with captcha session issues

Pride Grimm

Pride Grimm

Oct 19 2012 9:12 PM
1.5k

I'm trying to write a simple captcha program in vb.net. I'm just wanting to make an image from random numbers and display it, check the answer, and then proceed. I'm pretty new to vb.net, so I found some code to generate the information. I will cite the owner when I find it again (http://www.knowlegezone.com/80/article/Technology/Software/Asp-Net/Simple-ASP-NET-CAPTCHA-Tutorial)

This is in the onload() of default2.aspx

 Public Sub returnNumer()

   
Dim num1 As New Random
   
Dim num2 As New Random

   
Dim numQ1 As Integer
   
Dim numQ2 As Integer
   
Dim QString As String


    numQ1
= num1.Next(10, 15)
    numQ2
= num2.Next(17, 31)



    QString
= numQ1.ToString + " + " + numQ2.ToString + " = "
    Session
("answer") = numQ1 + numQ2


   
Dim bitmap As New Bitmap(85, 35)
   
Dim Grfx As Graphics = Graphics.FromImage(bitmap)
   
Dim font As New Font("Arial", 18, FontStyle.Bold, GraphicsUnit.Pixel)
   
Dim Rect As New Rectangle(0, 0, 100, 50)

    Grfx
.FillRectangle(Brushes.Brown, Rect)
    Grfx
.DrawRectangle(Pens.PeachPuff, Rect) ' Border
    Grfx
.DrawString(QString, font, Brushes.Azure, 0, 0)




    Response
.ContentType = "Image/jpeg"
    bitmap
.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg)


    bitmap
.Dispose()
    Grfx
.Dispose()



End Sub

So I put this in a separate page, like this

This all works find and dandy, but when I get the answer from the session like this

Dim literal As String = Convert.ToString(Session("answer"))

It's always one behind. So if The images adds to 32, the answer in session isn't 32. But after a refresh (and a new image) the session("answer") will be 32. Is there a way to refresh the session on page 1, after the default2.aspx loads? Is there a better way to do this?

I though about trying to run the code all on one page, and trying to set the src of and image to returnNumber(), but I need a bit of help on that one.