.net中验证码的几种常用方法
验证码功能一般是用于防止批量注册的,不少网站为了防止用户利用机器人自动注册、登录、灌水,都采用了验证码技术。所谓验证码,就是将一串随机产生的数字或字母或符号或文字,生成一幅图片, 图片里加上一些干扰象素(防止OCR),由用户肉眼识别其中的验证码信息,输入表单提交网站验证,验证成功后才能使用某项功能。
常见的验证码有如下几种:
1、纯数字验证码,一般为四位随机数字;
2、数字+字母验证码,一般从数字(0~9)和字母(A~Z和a~z)中随机抽出几个字符组成;
3、汉字验证码,相对而言,这种验证码比较少见一点,实现起来也相对复杂一些,但在不少网站中还是可以看到的;
一、纯数字验证码的实现
(1)使用随机数方式,代码如下:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 view plaincopy to clipboardprint?
2 private String GetRandomint(int codeCount)
3 {
4 Random random = new Random();
5 string min = "";
6 string max = "";
7 for (int i = 0; i < codeCount; i++)
8 {
9 min +="1";
10 max+="9";
11 }
12 return (random.Next(Convert.ToInt32(min),Convert.ToInt32(max)).ToString());
13 }
14 private String GetRandomint(int codeCount)
15 {
16 Random random = new Random();
17 string min = "";
18 string max = "";
19 for (int i = 0; i < codeCount; i++)
20 {
21 min +="1";
22 max+="9";
23 }
24 return (random.Next(Convert.ToInt32(min),Convert.ToInt32(max)).ToString());
25 }
(2)使用随机组合方式,代码如下
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 view plaincopy to clipboardprint?
2 private string CreateRandomCode(int codeCount)
3 {
4 string allChar = "0,1,2,3,4,5,6,7,8,9";
5 string[] allCharArray = allChar.Split(',');
6 string randomCode = "";
7 int temp = -1;
8 Random rand = new Random();
9 for (int i = 0; i < codeCount; i++)
10 {
11 if (temp != -1)
12 {
13 rand = new Random(i * temp * ((int)DateTime.Now.Ticks));
14 }
15 int t = rand.Next(9);
16 if (temp == t)
17 {
18 return CreateRandomCode(codeCount);
19 }
20 temp = t;
21 randomCode += allCharArray[t];
22 }
23 return randomCode;
24 }
25 private string CreateRandomCode(int codeCount)
26 {
27 string allChar = "0,1,2,3,4,5,6,7,8,9";
28 string[] allCharArray = allChar.Split(',');
29 string randomCode = "";
30 int temp = -1;
31 Random rand = new Random();
32 for (int i = 0; i < codeCount; i++)
33 {
34 if (temp != -1)
35 {
36 rand = new Random(i * temp * ((int)DateTime.Now.Ticks));
37 }
38 int t = rand.Next(9);
39 if (temp == t)
40 {
41 return CreateRandomCode(codeCount);
42 }
43 temp = t;
44 randomCode += allCharArray[t];
45 }
46 return randomCode;
47 }
2、数字+字母验证码的实现
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 view plaincopy to clipboardprint?
2 private string CreateRandomCode(int codeCount)
3 {
4 string allChar = "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,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";
5 string[] allCharArray = allChar.Split(',');
6 string randomCode = "";
7 int temp = -1;
8 Random rand = new Random();
9 for (int i = 0; i < codeCount; i++)
10 {
11 if (temp != -1)
12 {
13 rand = new Random(i * temp * ((int)DateTime.Now.Ticks));
14 }
15 int t = rand.Next(61);
16 if (temp == t)
17 {
18 return CreateRandomCode(codeCount);
19 }
20 temp = t;
21 randomCode += allCharArray[t];
22 }
23 return randomCode;
24 }
25 private string CreateRandomCode(int codeCount)
26 {
27 string allChar = "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,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";
28 string[] allCharArray = allChar.Split(',');
29 string randomCode = "";
30 int temp = -1;
31 Random rand = new Random();
32 for (int i = 0; i < codeCount; i++)
33 {
34 if (temp != -1)
35 {
36 rand = new Random(i * temp * ((int)DateTime.Now.Ticks));
37 }
38 int t = rand.Next(61);
39 if (temp == t)
40 {
41 return CreateRandomCode(codeCount);
42 }
43 temp = t;
44 randomCode += allCharArray[t];
45 }
46 return randomCode;
47 }
48