深入学习.net验证码生成及使用方法
程序员文章站
2024-02-12 14:35:22
小课堂:验证码的作用:
几年前,大部分网站、论坛之类的是没有验证码的,因为对于一般用户来说验证码只是增加了用...
小课堂:验证码的作用:
几年前,大部分网站、论坛之类的是没有验证码的,因为对于一般用户来说验证码只是增加了用户的操作,降低了用户的体验。但是后来各种灌水机器人、投票机器人、恶意注册机器人层出不穷,大大增加了网站的负担同时也给网站数据库带来了大量的垃圾数据。为了防止各种机器人程序的破坏,于是程序员想出了只有人眼能够识别的,程序不容易识别的验证码!
验证码是一个图片,将字母、数字甚至汉字作为图片的内容,这样一张图片中的内容用人眼很容易识别,而程序将无法识别。在进行数据库操作之前(比如登录验证、投票、发帖、回复、注册等等)程序首先验证客户端提交的验证码是否与图片中的内容相同,如果相同则进行数据库操作,不同则提示验证码错误,不进行数据库操作。这样各种机器人程序就被拒之门外了!
但是随着计算机科学的发展,模式识别等技术越来越成熟,于是编写机器人程序的家伙可以通过程序将直接写在图片中的内容识别出来,然后提交到服务器,这样验证码将形同虚设。为了防止机器人程序的识别,验证码的图片生成也不断在发展,加入干扰点、干扰线,文字变形、变换角度位置,颜色不同……各种防止计算机识别的技术也应用到验证码中。就在这两种技术的竞争中,于是便形成了我们现在看到的验证码,已经有很多人在抱怨“这是什么验证码哦,人眼都分辨不清楚是什么”,一切也是无奈。
了解了验证码的作用,下面写一个简单的验证码生成及使用的实例
先建个页面用来展示验证码和判断验证码输入是否正确
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <script src="scripts/jquery-1.4.1.min.js" type="text/javascript"></script> <script type="text/javascript"> //点击切换验证码 function f_refreshtype() { var image1 = document.getelementbyid("img"); if (image1 != null) { image1.src = image1.src + "?"; } } </script> </head> <body> <form id="form1" runat="server"> <div> <table> <tr> <td> <asp:textbox id="textbox1" runat="server"></asp:textbox> </td> <td> <img src="png.aspx" id="img" onclick="f_refreshtype()" /> </td> <td> <asp:button id="button1" runat="server" text="确定" /> </td> </tr> </table> </div> </form> </body> </html>
此页面后台对验证码进行验证
protected void page_load(object sender, eventargs e) { //生成的验证码被保存到session中 if (session["checkcode"] != null) { string checkcode = session["checkcode"].tostring(); if (this.textbox1.text == checkcode) { clientscript.registerclientscriptblock(this.gettype(), "", "alert('验证码输入正确!')", true); } else { clientscript.registerclientscriptblock(this.gettype(), "", "alert('验证码输入错误!')", true); } } }
生成验证码页面png.aspx
protected void page_load(object sender, eventargs e) { if (!ispostback) { createcheckcodeimage(generatecheckcodes(4)); } } public void showauthcode(stream stream, out string code) { random random = new random(); code = random.next(1000, 9999).tostring(); bitmap bitmap = createauthcode(code); bitmap.save(stream, system.drawing.imaging.imageformat.gif); } private string generatecheckcodes(int icount) { int number; string checkcode = string.empty; int iseed = datetime.now.millisecond; system.random random = new random(iseed); for (int i = 0; i < icount; i++) { number = random.next(10); checkcode += number.tostring(); } session["checkcode"] = checkcode; return checkcode; } private bitmap createauthcode(string str) { font fn = new font("宋体", 12); brush forecolor = brushes.black; brush bgcolor = brushes.white; pointf pf = new pointf(5, 5); bitmap bitmap = new bitmap(100, 25); rectangle rec = new rectangle(0, 0, 100, 25); graphics gh = graphics.fromimage(bitmap); gh.fillrectangle(bgcolor, rec); gh.drawstring(str, fn, forecolor, pf); return bitmap; } private void createcheckcodeimage(string checkcode) { if (checkcode == null || checkcode.trim() == string.empty) return; int iwordwidth = 15; int iimagewidth = checkcode.length * iwordwidth; bitmap image = new bitmap(iimagewidth, 20); graphics g = graphics.fromimage(image); try { //生成随机生成器 random random = new random(); //清空图片背景色 g.clear(color.white); //画图片的背景噪音点 for (int i = 0; i < 20; 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); } //画图片的背景噪音线 for (int i = 0; i < 2; i++) { int x1 = 0; int x2 = image.width; int y1 = random.next(image.height); int y2 = random.next(image.height); if (i == 0) { g.drawline(new pen(color.gray, 2), x1, y1, x2, y2); } } for (int i = 0; i < checkcode.length; i++) { string code = checkcode[i].tostring(); int xleft = iwordwidth * (i); random = new random(xleft); int iseed = datetime.now.millisecond; int ivalue = random.next(iseed) % 4; if (ivalue == 0) { font font = new font("arial", 13, (fontstyle.bold | system.drawing.fontstyle.italic)); rectangle rc = new rectangle(xleft, 0, iwordwidth, image.height); lineargradientbrush brush = new lineargradientbrush(rc, color.blue, color.red, 1.5f, true); g.drawstring(code, font, brush, xleft, 2); } else if (ivalue == 1) { font font = new system.drawing.font("楷体", 13, (fontstyle.bold)); rectangle rc = new rectangle(xleft, 0, iwordwidth, image.height); lineargradientbrush brush = new lineargradientbrush(rc, color.blue, color.darkred, 1.3f, true); g.drawstring(code, font, brush, xleft, 2); } else if (ivalue == 2) { font font = new system.drawing.font("宋体", 13, (system.drawing.fontstyle.bold)); rectangle rc = new rectangle(xleft, 0, iwordwidth, image.height); lineargradientbrush brush = new lineargradientbrush(rc, color.green, color.blue, 1.2f, true); g.drawstring(code, font, brush, xleft, 2); } else if (ivalue == 3) { font font = new system.drawing.font("黑体", 13, (system.drawing.fontstyle.bold | system.drawing.fontstyle.bold)); rectangle rc = new rectangle(xleft, 0, iwordwidth, image.height); lineargradientbrush brush = new lineargradientbrush(rc, color.blue, color.green, 1.8f, true); g.drawstring(code, font, brush, xleft, 2); } } //////画图片的前景噪音点 //for (int i = 0; i < 8; 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.binarywrite(ms.toarray()); } finally { g.dispose(); image.dispose(); } }