C 使用者登入時的驗證碼程式

2022-08-01 16:18:14 字數 2736 閱讀 8768

在使用者登入的窗體中加入驗證碼程式,就像網頁登入那樣的「亂碼」

其實主要的程式很簡單,只有兩部分:

1 通過random生成隨機的驗證內容

2通過draw將random內容轉化成格式顯示出來

生成隨機的驗證內容:

private

string

checkcode()

//此方法生成

return

checkcode;

//返回生成的字串

}

再下來是生成了,中一般就是隨機的改變一下背景,製造噪音點,**

private

void

codeimage(

string

checkcode)

font font

=new

system.drawing.font(

"arial",

12, (system.drawing.fontstyle.bold));

g.drawstring(checkcode, font,

newsolidbrush(color.red), 2,

2);for(

inti =0

; i

<

150; i

++)

//畫的前景噪音點

//畫的邊框線

g.drawrectangle(

newpen(color.silver), 0,

0, image.width -1

, image.height -1

);this

.picturebox1.width

=image.width;

//設定picturebox的寬度

this

.picturebox1.height

=image.height;

//設定picturebox的高度

this

.picturebox1.backgroundimage

=image;

//設定picturebox的背景影象

}catch

}

最後呢 就是在你要觸發這個「亂碼」的地方呼叫就行了,一般如「驗證失敗後」、「點選重新整理另乙個時

codeimage(checkcode());

當然,有時我們還會看到有些驗證碼中會有些是漢字的,我們也能實現,就是要用到漢字的區位碼

public

static

object

createcode(

intstrlength)

;random rnd

=new

random();

//定義乙個object陣列用來

object

bytes

=new

object

[strlength];

/**/

/*每迴圈一次產生乙個含兩個元素的十六進製制位元組陣列,並將其放入bject陣列中

每個漢字有四個區位碼組成

區位碼第1位和區位碼第2位作為位元組陣列第乙個元素

區位碼第3位和區位碼第4位作為位元組陣列第二個元素

*/for

(inti =

0; i

<

strlength; i++)

else

if(r3

==15

)else

string

str_r4

=r[r4].trim();

//定義兩個位元組變數儲存產生的隨機漢字區位碼

byte

byte1

=convert.tobyte(str_r1

+str_r2,

16);

byte

byte2

=convert.tobyte(str_r3

+str_r4,

16);

//將兩個位元組變數儲存在位元組陣列中

byte

str_r

=new

byte

;//將產生的乙個漢字的位元組陣列放入object陣列中

bytes.setvalue(str_r, i);

}return

bytes;

}

通過這段**就能生成隨機的漢字了

然後用生成的漢字和隨機的數字、字母就能生成帶有漢字的驗證碼了。

將checkcode()**改變下:

private

string

checkcode()

//此方法生成

else

//加入漢字的部分

//累加字串

}return

checkcode;

//返回生成的字串

}

但是問題是 生成的漢字有些不認識,驗證很鬱悶。不建議使用!!!

登入驗證碼C

一.新建乙個checkcode.aspx using system using system.data using system.configuration using system.collections using system.web using system.web.security usi...

登入驗證碼

生成驗證碼 指定驗證碼的長度 public static string createvalidatecode int length 生成隨機數字 for int i 0 i length i 抽取隨機數字 for int i 0 i length i 生成驗證碼 for int i 0 i leng...

使用者登入簡訊驗證碼的實現

目前比較流行的登入方式就是簡訊驗證碼登入,但是這種方式是需要借助第三方簡訊提供商 即為付費的官方介面 其實現流程如下 具體實現如下 let md5 require blueimp md5 let moment require moment let base64 require js base64 b...