网站登录简单验证码

 1  public string BindCode(int length)
 2         {
 3             string Vchar = "1,2,3,4,5,6,7,8,9,a,b,c,d,e,f,g,h,i,j,k,l,m,n,p,q,r,s,t,u,v,w,x,y,z,A,B,C,D,E,F,G,H,I,J,K,L,M,N,P,Q,R,S,T,U,V,W,X,Y,Z";
 4 
 5             string[] VcArray = Vchar.Split(new Char[] { ',' });         
 8             Random rand = new Random(); 
10            
11             int a;
12             List<int> result = new List<int>();
13             string numlist = string.Empty;
14 
15             for (int i = 1; i < length + 1; i++)
16 
17             {
18                 a = rand.Next(1, VcArray.Length);  //设置范围。我这里是取数据库的数据1~n
19                 if (result.Contains(a))
20                 {
21                     i = i - 1; //有重复值,重新再来一次,循环次数减一次
22                 }
23                 else
24                 {
25                     numlist += VcArray[a];  //无重复值,就添加在集合内
26                 }
27             }
28          
29             return numlist;         
30         }

调用:

1  protected void Page_Load(object sender, EventArgs e)
2         {
3             if (!IsPostBack)
4             {
5                 Barcoded.Text = BindCode(4);
6             }
7         }
原文地址:https://www.cnblogs.com/bonnie-w/p/8440582.html