您现在的位置: 无忧电子商务网 >> 信息学院 >> 程序开发 >> asp.net >> 正文

Asp.net驗證碼3/10

作者:作者:未…    信息学院来源:网络收集    点击数:    更新时间:2006-8-28 我要参与讨论

   

之前找了一個C#的驗證碼參考,但有一句用VB.NET卻找不出該怎樣寫,以致好長一段時間我的驗證碼都是重復的,今天在小鋪網友的耐心指點下,用 Dim aa As Integer = CType(DateTime.Now.Ticks Mod System.Int32.MaxValue, Integer)作了Random的種子,但是設中斷情況下執行才正常,若沒設中斷則會出現System.StackOverflowException' 的未處理例外狀況發生於 mscorlib.dll的錯誤

於是修正代碼如下,總算跑出自己尚算滿意的結果來

Dim allChar 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,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,W,X,Y,Z"
        Dim allCharArray() As String = allChar.Split(",")
        Dim randomCode As String = ""
        Dim temp As Integer = -1
        Dim rand As Random = New Random
        Dim i As Integer = 0
        Do While (i < codeCount)
            If (temp <> -1) Then
                'rand = New Random(i * temp)若沒有下面僅這裏會時常重復的
                Dim aa As Integer = CType(DateTime.Now.Ticks Mod System.Int32.MaxValue, Integer)'用VB.NET寫法,去掉了與I和TEMP的相乘

                rand = New Random(aa)
            End If
            Dim t As Integer = rand.Next(61) + 1'+1是改寫,61表示字符的總個數,索引值從0開始
            If t > allCharArray.Length - 1 Then t = allCharArray.Length - 1'改寫
            If temp = t Then'抑止驗證碼的連續重復
                '    Return createrandomcode(codecount)出現mscorlib.dll的死胡同
                i -= 1'改寫
                randomCode = Microsoft.VisualBasic.Left(randomCode, i)'改寫

            End If
            temp = t
            randomCode = randomCode + allCharArray(t)
            i += 1
        Loop

        Return randomCode

盡信書則不如無書,這是我最想說的一句話,不要讓信條束縛了自己的思想喔^__^

在google里搜索更多Asp.net驗證碼3/10

Google
Web www.51ec.org
【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
我来说两句 对此文章发表了评论
  昵 称: *必填    ·注册用户·
  评 分: 1分 2分 3分 4分 5分     严禁发表危害国家安全、政治、黄色淫秽等内容的评论,用户需对自己在使用本网站服务过程中的行为承担法律责任。本站管理员有权保留或删除评论内容,评论内容只代表机友个人观点,与本网站立场无关。  
评 论
内 容

 
评论列表 (最新 评论仅限网友观点!)

供求信息




| 设为首页 | 加入收藏 | 关于我们 | 广告服务 | 联系方式 | 友情链接 | 版权申明