C#验证码技术示例

本文介绍了一种使用C#生成验证码图片的方法,并演示了如何在网页中进行验证码的生成和验证过程。通过随机生成字符并绘制到图片上,同时在客户端设置Cookies保存验证码值,确保了验证码的安全性和有效性。
验证码生成页面:CheckCode.aspx
c# 代码
 
  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. public partial class CheckCode : System.Web.UI.Page
  13. {
  14. protected void Page_Load(object sender, EventArgs e)
  15. {
  16. this.CreateCheckCodeImage(GenerateCheckCode());
  17.  
  18. }
  19. private string GenerateCheckCode()
  20. {
  21. int number;
  22. char code;
  23. string checkCode = String.Empty;
  24.  
  25. System.Random random = new Random();
  26.  
  27. for (int i = 0; i < 5; i++)
  28. {
  29. number = random.Next();
  30.  
  31. if (number % 2 == 0)
  32. code = (char)(0 + (char)(number % 10));
  33. else
  34. code = (char)(A + (char)(number % 26));
  35.  
  36. checkCode += code.ToString();
  37. }
  38.  
  39. Response.Cookies.Add(new HttpCookie("CheckCode", checkCode));
  40.  
  41. return checkCode;
  42. }
  43.  
  44. private void CreateCheckCodeImage(string checkCode)
  45. {
  46. if (checkCode == null || checkCode.Trim() == String.Empty)
  47. return;
  48.  
  49. System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 22);
  50. Graphics g = Graphics.FromImage(image);
  51.  
  52. try
  53. {
  54. //生成随机生成器
  55. Random random = new Random();
  56.  
  57. //清空图片背景色
  58. g.Clear(Color.White);
  59.  
  60. //画图片的背景噪音线
  61. for (int i = 0; i < 25; i++)
  62. {
  63. int x1 = random.Next(image.Width);
  64. int x2 = random.Next(image.Width);
  65. int y1 = random.Next(image.Height);
  66. int y2 = random.Next(image.Height);
  67.  
  68. g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
  69. }
  70.  
  71. Font font = new System.Drawing.Font("Arial", 12, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic));
  72. 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);
  73. g.DrawString(checkCode, font, brush, 2, 2);
  74.  
  75. //画图片的前景噪音点
  76. for (int i = 0; i < 100; i++)
  77. {
  78. int x = random.Next(image.Width);
  79. int y = random.Next(image.Height);
  80.  
  81. image.SetPixel(x, y, Color.FromArgb(random.Next()));
  82. }
  83.  
  84. //画图片的边框线
  85. g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
  86.  
  87. System.IO.MemoryStream ms = new System.IO.MemoryStream();
  88. image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
  89. Response.ClearContent();
  90. Response.ContentType = "image/Gif";
  91. Response.BinaryWrite(ms.ToArray());
  92. }
  93. finally
  94. {
  95. g.Dispose();
  96. image.Dispose();
  97. }
  98. }
  99.  
  100.  
  101.  
  102. }

而在目标页面调用是用这样的:

请把验证码输入右边文本框中!

如果判断验证码呢,下面一段就是判断语句
c# 代码
 
  1. protected void btnSaveAs_Click(object sender, EventArgs e)
  2. {
  3. if (Request.Cookies["CheckCode"] == null)
  4. {
  5. lblMessage.Text = "您的浏览器设置已被禁用 Cookies,您必须设置浏览器允许使用 Cookies 选项后才能使用本系统。";
  6. lblMessage.Visible = true;
  7. return;
  8. }
  9.  
  10. if (String.Compare(Request.Cookies["CheckCode"].Value, txtCheckCode.Text, true) != 0)
  11. {
  12. lblMessage.Text = "验证码错误,请输入正确的验证码。";
  13. lblMessage.Visible = true;
  14. return;
  15. }
  16. }

转载于:https://www.cnblogs.com/gllgsoft/archive/2008/08/06/1262177.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值