menu 极光萝卜
【ASP.NET】练习笔记:验证码
folder_special 笔记    access_time 2016-7-31   remove_red_eye 1794   comment 0

前两天终于把学ASP.NET的那个PDF主要内容看完了,然后这两天在半学半偷懒中跟着PDF敲了个注册登录验证模块。

调试完后,感觉比较有学到的就是验证码的制作和使用。

其次就是发现这书果然不严谨,有些应该说明的却漏了说明,我在测试注册的时候死活注册失败,然后断点调试的时候才发现在SQL插入语句出了问题,ID不能为空。这里ID在我理解应该是自动生成的,但书上只简单的写了数据表的字段名,类型和主键,没有讲解建表过程,所以我最开始建表并没有注意到问题...导致各种注册失败。在经过百度后总算解决了,将ID设置标识,并设置标识种子和标识增量。

这里主要记录下生成验证码的aspx文件代码,方便以后需要时来查看和改进利用

生成的验证码示例:

v.png


using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace 注册登录练习
{
    public partial class ValidateNum : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string validateNum = CreateRandomNum(4);
            CreateIMG(validateNum);
            Session["ValidateNum"] = validateNum;
        }

        private string CreateRandomNum(int NumCount)
        {
            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,V,W,X,Y,Z";
            string[] allCharArray = allChar.Split(',');
            string randomNum = "";
            int temp = -1;
            Random rand = new Random();
            for(int i = 0; i < NumCount; i++)
            {
                if (temp != -1)
                {
                    rand = new Random(i * temp * ((int)DateTime.Now.Ticks));
                }
                int t = rand.Next(35);
                if (temp == t)
                {
                    return CreateRandomNum(NumCount);
                }
                temp = t;
                randomNum += allCharArray[t];
            }
            return randomNum;
        }

        private void CreateIMG(string validateNum)
        {
            if(validateNum==null || validateNum.Trim() == string.Empty)
            {
                return;
            }
            Bitmap image = new Bitmap(validateNum.Length * 12 + 10, 22);
            Graphics g = Graphics.FromImage(image);
            try
            {
                Random random = new Random();
                g.Clear(Color.White);
                //绘制背景噪音线
                for(int i = 0; i < 25; i++)
                {
                    int x1 = random.Next(image.Width);
                    int x2 = random.Next(image.Width);
                    int y1 = random.Next(image.Height);
                    int y2 = random.Next(image.Height);
                    g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
                }
                Font font = new Font("Arial", 12, (FontStyle.Bold | FontStyle.Italic));
                System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.Red, 1.2f, true);
                g.DrawString(validateNum, font, brush, 2, 2);
                //绘制前景噪音点
                for(int i = 0; i < 100; i++)
                {
                    int x = random.Next(image.Width);
                    int y = random.Next(image.Height);
                    image.SetPixel(x, y, Color.FromArgb(random.Next()));
                }
                //绘制边框线
                g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
                System.IO.MemoryStream ms = new System.IO.MemoryStream();
                image.Save(ms,System.Drawing.Imaging.ImageFormat.Gif);
                Response.ClearContent();
                Response.ContentType = "image/Gif";
                Response.BinaryWrite(ms.ToArray());
            }
            finally
            {
                g.Dispose();
                image.Dispose();
            }
        }
    }
}


local_offer C# ASP.NET