ASP.NET验证码(3种)

来源:互联网 发布:mac草叶壁纸 编辑:程序博客网 时间:2024/06/06 08:42

ASP.NET验证码(3种)

把最近碰到的 能够用的验证码 都放出来,作个记录
1.GSC_WebControlLibrary 这是在网上找到的一个控件,非常好用。但是效果不是特别好(见下图。
)虽然容易使用,所有的属性都可以像控件一样设置,但是可用性不太高。用户不能自定义,而且看起来这个验证码效果不太好。
效果:

2.用一个页面生成图片,另一个页面调用,验证码存入cookie,调用时取cookie对比验证.这个用户就可以按自己的喜好更改效果和验证码的长度了 (:

效果如图:


代码如下:
CheckCode.aspx

  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Collections;
  5. using System.Web;
  6. using System.Web.Security;
  7. using System.Web.UI;
  8. using System.Web.UI.WebControls;
  9. using System.Web.UI.WebControls.WebParts;
  10. using System.Web.UI.HtmlControls;
  11. using System.Drawing;
  12. using System.Drawing.Drawing2D;
  13. using System.Drawing.Imaging;
  14. public partial class Tools_CheckCode : System.Web.UI.Page
  15. {
  16.     protected void Page_Load(object sender, EventArgs e)
  17.     {
  18.         this.CreateCheckCodeImage(GenerateCheckCode());
  19.     }
  20.     private string GenerateCheckCode()
  21.     {
  22.         int number;
  23.         char code;
  24.         string checkCode = String.Empty;
  25.         System.Random random = new Random();
  26.         for (int i = 0; i < 5; i++)
  27.         {
  28.             number = random.Next();
  29.             if (number % 2 == 0)
  30.                 code = (char)('0' + (char)(number % 10));
  31.             else
  32.                 code = (char)('A' + (char)(number % 26));
  33.             checkCode += code.ToString();
  34.         }
  35.         Response.Cookies.Add(new HttpCookie("CheckCode", checkCode));
  36.         return checkCode;
  37.     }
  38.     private void CreateCheckCodeImage(string checkCode)
  39.     {
  40.         if (checkCode == null || checkCode.Trim() == String.Empty)
  41.             return;
  42.         System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 22);
  43.         Graphics g = Graphics.FromImage(image);
  44.         try
  45.         {
  46.             //生成随机生成器
  47.             Random random = new Random();
  48.             //清空图片背景色
  49.             g.Clear(Color.White);
  50.             //画图片的背景噪音线
  51.             for (int i = 0; i < 25; i++)
  52.             {
  53.                 int x1 = random.Next(image.Width);
  54.                 int x2 = random.Next(image.Width);
  55.                 int y1 = random.Next(image.Height);
  56.                 int y2 = random.Next(image.Height);
  57.                 g.DrawLine(new Pen(Color.GreenYellow), x1, y1, x2, y2);
  58.             }
  59.             Font font = new System.Drawing.Font("Verdana", 12, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic));
  60.             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);
  61.             g.DrawString(checkCode, font, brush, 2, 2);
  62.             //画图片的前景噪音点
  63.             for (int i = 0; i < 80; i++)
  64.             {
  65.                 int x = random.Next(image.Width);
  66.                 int y = random.Next(image.Height);
  67.                 image.SetPixel(x, y, Color.FromArgb(random.Next()));
  68.             }
  69.             //画图片的边框线
  70.             g.DrawRectangle(new Pen(Color.Red), 0, 0, image.Width - 1, image.Height - 1);
  71.             System.IO.MemoryStream ms = new System.IO.MemoryStream();
  72.             image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
  73.             Response.ClearContent();
  74.             Response.ContentType = "image/Gif";
  75.             Response.BinaryWrite(ms.ToArray());
  76.         }
  77.         finally
  78.         {
  79.             g.Dispose();
  80.             image.Dispose();
  81.         }
  82.     }
  83. }

然后在需要使用的页面引用:
UseCheckCode.aspx

  1. <img src="Tools/CheckCode.aspx" alt="验证码" style="width: 60px; height: 24px" />

3.用web handler生成图片。这个其实和前面的意思大致差不多,调用方法也基本和2一样,不同的是,他的验证码是存入Session的。供学习参考。

效果图如下:


ValidateImageHandler.ashx