asp.net网页中生成随机验证码

asp.net生成验证码的方法:

 public string RandomNum(int n) //
    {
        //定义一个包括数字、大写英文字母和小写英文字母的字符串
        string strchar = "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,V,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";
        //将strchar字符串转化为数组
        //String.Split 方法返回包含此实例中的子字符串(由指定Char数组的元素分隔)的 String 数组。
        string[] VcArray = strchar.Split(',');
        string VNum = "";
        //记录上次随机数值,尽量避免产生几个一样的随机数           
        int temp = -1;                       
        //采用一个简单的算法以保证生成随机数的不同
        Random rand = new Random();
        for (int i = 1; i < n + 1; i++)
        {
            if (temp != -1)
            {
                //unchecked 关键字用于取消整型算术运算和转换的溢出检查。
                //DateTime.Ticks 属性获取表示此实例的日期和时间的刻度数。
                rand = new Random(i * temp * unchecked((int)DateTime.Now.Ticks));
            }
            //Random.Next 方法返回一个小于所指定最大值的非负随机数。
            int t = rand.Next(61);
            if (temp != -1 && temp == t)
            { 
                return RandomNum(n);
            }
            temp = t;
            VNum += VcArray[t];
        }
        return VNum;//返回生成的随机数
    }

 然后在网页代码中调用刚才的方法:

 1  string checkCode = "你需要生成的验证码";                                    //声明字符串并赋值
 2             System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 20.5)), 22);                                                                //创建画板
 3             Graphics g = Graphics.FromImage(image);    //创建Graphics对象
 4             try
 5             {
 6                 Random random = new Random();                //生成随机生成器
 7                 g.Clear(Color.White);                            //清空图片背景色
 8                 //画图片的背景噪音线
 9                 for (int i = 0; i < 2; i++)
10                 {
11                     int x1 = random.Next(image.Width);
12                     int x2 = random.Next(image.Width);
13                     int y1 = random.Next(image.Height);
14                     int y2 = random.Next(image.Height);
15                     g.DrawLine(new Pen(Color.Black), x1, y1, x2, y2);
16                 }
17                 Font font = new System.Drawing.Font("Arial", 12, (System.Drawing.FontStyle.Bold));
18                 System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true);
19                 g.DrawString(checkCode, font, brush, 2, 2);
20                 //画图片的前景噪音点
21                 for (int i = 0; i < 100; i++)
22                 {
23                     int x = random.Next(image.Width);
24                     int y = random.Next(image.Height);
25                     image.SetPixel(x, y, Color.FromArgb(random.Next()));
26                 }
27                 //画图片的边框线
28                 g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
29                 System.IO.MemoryStream ms = new System.IO.MemoryStream();
30                 image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
31                 Response.ClearContent();
32                 Response.ContentType = "image/Gif";
33                 Response.BinaryWrite(ms.ToArray());
34             }
35             finally
36             {
37                 g.Dispose();
38                 image.Dispose();
39             }

这样就可以生成的验证码是一张图片了,可以防止注册机的恶意注册了!!!可能会有更好的方法,本人是个初级学员,望高手看到后多多指教,谢谢!!!

原文地址:https://www.cnblogs.com/liangdelin/p/2477055.html