深入学习.net验证码生成及使用方法

所属分类: 网络编程 / ASP.NET 阅读数: 1695
收藏 0 赞 0 分享

小课堂:验证码的作用:

      几年前,大部分网站、论坛之类的是没有验证码的,因为对于一般用户来说验证码只是增加了用户的操作,降低了用户的体验。但是后来各种灌水机器人、投票机器人、恶意注册机器人层出不穷,大大增加了网站的负担同时也给网站数据库带来了大量的垃圾数据。为了防止各种机器人程序的破坏,于是程序员想出了只有人眼能够识别的,程序不容易识别的验证码!

      验证码是一个图片,将字母、数字甚至汉字作为图片的内容,这样一张图片中的内容用人眼很容易识别,而程序将无法识别。在进行数据库操作之前(比如登录验证、投票、发帖、回复、注册等等)程序首先验证客户端提交的验证码是否与图片中的内容相同,如果相同则进行数据库操作,不同则提示验证码错误,不进行数据库操作。这样各种机器人程序就被拒之门外了!

      但是随着计算机科学的发展,模式识别等技术越来越成熟,于是编写机器人程序的家伙可以通过程序将直接写在图片中的内容识别出来,然后提交到服务器,这样验证码将形同虚设。为了防止机器人程序的识别,验证码的图片生成也不断在发展,加入干扰点、干扰线,文字变形、变换角度位置,颜色不同……各种防止计算机识别的技术也应用到验证码中。就在这两种技术的竞争中,于是便形成了我们现在看到的验证码,已经有很多人在抱怨“这是什么验证码哦,人眼都分辨不清楚是什么”,一切也是无奈。

了解了验证码的作用,下面写一个简单的验证码生成及使用的实例

先建个页面用来展示验证码和判断验证码输入是否正确

<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();
 }
 }

更多精彩内容其他人还在看

Asp.Net二级域名共享Forms身份验证、下载站/图片站的授权访问控制

我们平时一般在做图片或者文件下载权限控制的时候基本都是控制到下载页面的,当你的下载地址暴露后,浏览者就直接可以通过文件地址进行下载了,这时候也就出现了我们常说的盗链
收藏 0 赞 0 分享

在ASP.NET中下载文件的实现代码

通过ASP.NET来下载文件,这个问题可大可小,我们先从小的开始。当我们要让用户下载一个文件
收藏 0 赞 0 分享

asp.net下日期和时间处理的类库

发一个专门处理时间和日期的类库,记录以备查询
收藏 0 赞 0 分享

LINQ重写博客垃圾图片回收算法

本人博客后台管理模块有个功能,可以扫描图片上传文件夹下所有未被引用的博客
收藏 0 赞 0 分享

C#多线程Singleton(单件)模式模板

下面是一个C#多线程单件模式的代码模板。把T换成你自己的类型就可以使用了。其精妙之处就在于用lock语句锁定资源来避免多线程同时走入if语句去创建多个对象
收藏 0 赞 0 分享

URL重写及干掉ASP.NET试图状态的实现方法

URL重写已经很普遍了,但基本上大部分的URL重写都不支持页面的相对路径,所有如果想在已经开发好的项目中添加还是有压力的,第二就是例如微软的那个URL重写是根据正则表达式来处理的,那样是很好,但也有不足之处,就是不方便定位到某个页面只能有哪些参数
收藏 0 赞 0 分享

正则方式的自动小偷抓网程序

公司里面有许多数据没人去录入,做一个抓取网页的程序,以前做CMS系统的时候涉及过,不过这次的处理HTML上和以前做了些区别
收藏 0 赞 0 分享

asp.net生成缩略图实现代码

此文件imgSmall.ashx专门用来生成图片的缩略图,可以减少服务器压力,降低网络流量,初学者必备
收藏 0 赞 0 分享

asp.net richTextBox中高亮显示选中字符串或文本

最近开发程序需要对一段文本中的某个字符串进行高亮显示,网上找了下资料
收藏 0 赞 0 分享

ASP.net的验证控件浅析

前些天在做注册页面的验证的时候,用了下ASP.net的验证控件,有一些体会,特写下这篇博客,如果有朋友有不同ideas,欢迎大家留言
收藏 0 赞 0 分享
查看更多