上網查了一堆code… 有不少都是c#.net用的,怎麼都沒有vb.net的啦!

好不容易找到一篇,但是卻一直出現"數字運算導致溢位"
查了很久,才發現… 原來是 Random 出了點錯誤

'Validate Code
Dim Code() As String = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"}

網路上的版本:
Dim rd As New Random(Cint(DateTime.Now.Ticks))
...

正確版本:
Dim rd As New Random
...

因為 DateTime.Now 的時間轉成數字的時候,會超出 Integer 的長度限制
所以會有溢位的情況發生

另外在執行「亂數產生驗證文字」時
是利用 For 迴圈產生多個圖文字

Dim strRd As String = String.Empty

For i = 0 To 6 - 1 Step i + 1
            strRd += Code(rd.Next(35))    '35是 Code() 陣列中的文字個數(從0開始數)
Next

所以『strRd』是字串型別,故不需再把 Code 轉成 Int
錯誤版:strRd += CInt(Code(rd.Next(35)))

 

※ 完整 Code ※  (ValidateCode.aspx.vb)  <<aspx檔裡,什麼都不必做>>

Imports System
Imports System.Data
Imports System.Configuration
Imports System.Collections  
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Text
Imports System.IO
Imports System.Drawing.Imaging

Partial Class Drawing_0601_iPhone_ValidateCode
    Inherits System.Web.UI.Page

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

        'Validate Code
        Dim Code() As String = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"}
        Dim strRd As String = String.Empty

        Dim rd As New Random

        'Dim rd As Random = New Random(CType(DateTime.Now.Ticks, Integer))
        Dim Bmp As Drawing.Bitmap = New Drawing.Bitmap(80, 25)  '建立實體圖檔並設定大小
        Dim Gpi As Drawing.Graphics = Drawing.Graphics.FromImage(Bmp)

        Dim Font1 As Drawing.Font = New Drawing.Font("Verdana", 10, Drawing.FontStyle.Italic)

        Dim i As Integer
        For i = 0 To 6 - 1 Step i + 1
            strRd += Code(rd.Next(35))
        Next

        Dim PenLine As Drawing.Pen = New Drawing.Pen(Drawing.Color.Red, 1)  '實體化筆刷並設定顏色、大小(畫X,Y軸用)
        Gpi.Clear(Drawing.Color.White) '設定背景顏色
        Gpi.DrawLine(PenLine, 0, rd.Next(80), 90, rd.Next(25))
        Gpi.DrawString(strRd, Font1, Drawing.Brushes.Black, 0, 0)

        Dim j As Integer
        For j = 0 To 50 Step j + 1
            Dim RandPixelX As Integer = rd.Next(0, 80)
            Dim RandPixelY As Integer = rd.Next(0, 25)
            Bmp.SetPixel(RandPixelX, RandPixelY, Drawing.Color.Blue)    '加入霧點
        Next

        Session("ValidateCode") = strRd        '將驗證碼存入Session以便稍後進行驗證
        Bmp.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Gif)

    End Sub
End Class

 

■ 另一頁要User看到圖片文字,key in 的 aspx 檔裡,至少需要:
1. HTML 的 img
2. TextBox
3. Button

<img src=ValidateCode.aspx alt="Validate Code" border="1" style="position: static" />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
     <asp:RequiredFieldValidator ID="ImgText" runat="server" ErrorMessage="請輸入左圖中的文字"
              Font-Size="Small" ControlToValidate="TextBox1"></asp:RequiredFieldValidator></td>
<asp:Button ID="Msg_Post" runat="server" Text="發表此主題…" Font-Size="Small" Style="position: static" /></td>

 

■ User看到圖片文字,key in 的 vb檔裡,Button_OnClick :

If TextBox1.Text = CType(Session("ValidateCode"), String) Then
        cmd.ExecuteNonQuery()
        Response.Write("<SCRIPT LANGAUAGE=""javascript"">parent.location='導頁位置';alert(""訊息發表成功!!"");</script>")
Else
        Response.Write("<script>alert('驗證碼錯誤!!');</script>")
End If

 

■ 實例:三助網 - 看期刋抽 i-Phone

■ 參考網頁:
[.NET]圖片驗證防止灌水機器人
實作網頁圖片驗證碼
好好用的C# 與 VB.NET程式碼互轉

創作者介紹
創作者 ⒶⓂⓎ 的頭像
ⒶⓂⓎ

ㄚ咪的幸福收納盒

ⒶⓂⓎ 發表在 痞客邦 留言(0) 人氣()