如何实现用.net成功胜出验证码对于一些工作者来说可能比较迷茫;关于验证码,一般来说是为了能够法制一批批量注册的心怀不轨分子,自动注册、登陆灌水等。而验证码的额一般情况有如下几种1、 纯数字,一般是四位数字2、 数字加字幕形式3、 汉子验证码  二、验证码的实现  1、纯数字验证码的实现  纯数字验证码的实现相对比较简单,可通过以下两种方法来实现  (1)使用随机数方式,代码如下:         private String GetRandomint(int codeCount)        {            Random random = new Random();            string min = "";            string max = "";            for (int i = 0; i            {                min +="1";                max+="9";            }                return (random.Next(Convert.ToInt32(min),Convert.ToInt32(max)).ToString());        }   (2)使用随机组合方式,代码如下:        private string CreateRandomCode(int codeCount)        {            string allChar = "0,1,2,3,4,5,6,7,8,9";            string[] allCharArray = allChar.Split(',');            string randomCode = "";            int temp = -1;            Random rand = new Random();            for (int i = 0; i            {                if (temp != -1)                {                    rand = new Random(i * temp * ((int)DateTime.Now.Ticks));                }                int t = rand.Next(9);                if (temp == t)                {                    return CreateRandomCode(codeCount);                }                temp = t;                randomCode += allCharArray[t];            }            return randomCode;        }  2、数字+字母验证码的实现,代码如下:        private string CreateRandomCode(int codeCount)        {            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";            string[] allCharArray = allChar.Split(',');            string randomCode = "";            int temp = -1;            Random rand = new Random();            for (int i = 0; i            {                if (temp != -1)                {                    rand = new Random(i * temp * ((int)DateTime.Now.Ticks));                }                int t = rand.Next(61);                if (temp == t)                {                    return CreateRandomCode(codeCount);                }                temp = t;                randomCode += allCharArray[t];            }            return randomCode;        }  3、汉字验证码的实现,代码如下:        /**/        /*    此函数在汉字编码范围内随机创建含两个元素的十六进制字节数组,每个字节数组代表一个汉字,并将    四个字节数组存储在object数组中。    参数:strlength,代表需要产生的汉字个数    */        public static object[] CreateRegionCode(int strlength)        {            //定义一个字符串数组储存汉字编码的组成元素            string[] rBase = new String[16] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };            Random rnd = new Random();            //定义一个object数组用来            object[] bytes = new object[strlength];            /**/            /*每循环一次产生一个含两个元素的十六进制字节数组,并将其放入bject数组中         每个汉字有四个区位码组成         区位码第1位和区位码第2位作为字节数组第一个元素         区位码第3位和区位码第4位作为字节数组第二个元素        */            for (int i = 0; i            {                //区位码第1位                int r1 = rnd.Next(11, 14);                string str_r1 = rBase[r1].Trim();                //区位码第2位                rnd = new Random(r1 * unchecked((int)DateTime.Now.Ticks) + i);//更换随机数发生器的种子避免产生重复值                int r2;                if (r1 == 13)                {                    r2 = rnd.Next(0, 7);                }                else                {                    r2 = rnd.Next(0, 16);                }                string str_r2 = rBase[r2].Trim();                //区位码第3位                rnd = new Random(r2 * unchecked((int)DateTime.Now.Ticks) + i);                int r3 = rnd.Next(10, 16);                string str_r3 = rBase[r3].Trim();                //区位码第4位                rnd = new Random(r3 * unchecked((int)DateTime.Now.Ticks) + i);                int r4;                if (r3 == 10)                {                    r4 = rnd.Next(1, 16);                }                else if (r3 == 15)                {                    r4 = rnd.Next(0, 15);                }                else                {                    r4 = rnd.Next(0, 16);                }                string str_r4 = rBase[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[] { byte1, byte2 };                //将产生的一个汉字的字节数组放入object数组中                bytes.SetValue(str_r, i);            }            return bytes;        }以上内容由知码网为您推荐,推荐的内容可在我们知码网详细寻找;知码网专为高端验证码需求人士服务!
09-21 05:19