首页 > 编程 > .NET > 正文

Asp.net驗證碼

2024-07-10 13:11:35
字体:
来源:转载
供稿:网友

之前找了一個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

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

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表