一个能自由切换字符验证和简单数学运算验证方式的 验证码操作类
2011-04-22 19:04
495 查看
生成输出图形的代码是借鉴园子里童鞋的。
上图
点击查看代码
调用方法。
验证方法:
高手别喷 纯属交流。
运算符 偷懒鸟。只写了 + - *
上图
点击查看代码
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Drawing; using System.Drawing.Design; using System.Drawing.Drawing2D; using System.Drawing.Printing; using System.Drawing.Imaging; using System.IO; namespace TheCms.Common { /// <summary> /// 验证码生成类 /// </summary> public class Captcha { #region 属性 /// <summary> /// 验证码长度 /// </summary> private int _CodeLength=4; /// <summary> /// 验证码长度,Normal方式下才有用 /// </summary> public int CodeLength { get { return _CodeLength; } set { _CodeLength = value; } } /// <summary> /// 验证码类型(Normal,普通数字+字母验证。Operation,运算符验证。) /// </summary> public ValidateType ValidateType { get; set; } #endregion #region 生成验证码 /// <summary> /// 生成验证码 /// </summary> public void CreateCaptch() { Bitmap BitImage; BitImage=CreateImages(GenCode(),ValidateType); BitImage.Save(System.Web.HttpContext.Current.Response.OutputStream, ImageFormat.Jpeg); //释放对象 BitImage.Dispose(); } #endregion #region 生成输出图片 /// <summary> /// 画图片的背景图,干扰 /// </summary> /// <param name="checkCode"></param> /// <returns></returns> private Bitmap CreateImages(string checkCode, ValidateType type) { int step = 0; if (type == ValidateType.Operation) { step = 5;//运算字符比较大,所以字距要比较大 } else { step = 3; } int iwidth = (int)(checkCode.Length * (13 + step)); System.Drawing.Bitmap image = new System.Drawing.Bitmap(iwidth, 25); Graphics g = Graphics.FromImage(image); g.Clear(Color.White);//清除背景色 Color[] c = { Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Brown, Color.DarkCyan, Color.Purple };//定义随机颜色 string[] font = { "Verdana", "Microsoft Sans Serif", "Comic Sans MS", "Arial", "宋体" }; Random rand = new Random(); for (int i = 0; i < 50; i++) { int x1 = rand.Next(image.Width); int x2 = rand.Next(image.Width); int y1 = rand.Next(image.Height); int y2 = rand.Next(image.Height); g.DrawLine(new Pen(Color.LightGray, 1), x1, y1, x2, y2);//根据坐标画线 } for (int i = 0; i < checkCode.Length; i++) { int cindex = rand.Next(6); int findex = rand.Next(5); Font f = new System.Drawing.Font(font[findex], 13, System.Drawing.FontStyle.Bold); Brush b = new System.Drawing.SolidBrush(c[cindex]); int ii = 4; if ((i + 1) % 2 == 0) { ii = 2; } g.DrawString(checkCode.Substring(i, 1), f, b, 3 + (i * (12 + step)), ii); } g.DrawRectangle(new Pen(c[rand.Next(6)], 0), 0, 0, image.Width - 1, image.Height - 1); System.IO.MemoryStream ms = new System.IO.MemoryStream(); return image; } #endregion #region 验证验证码 /// <summary> /// 比较用户输入验证码(True验证码准确,False验证码错误。) /// </summary> /// <param name="InputCheckCode">待验证的验证码</param> /// <returns>True验证码准确,False验证码错误。</returns> public static bool CheckVerifyCode(string InputCheckCode) { return System.Web.HttpContext.Current.Session["VerifyCode"].ToString().ToLower() == InputCheckCode.ToLower(); } #endregion #region GenCode public string GenCode() { Random rd = new Random(); string sResult = ""; if (ValidateType == ValidateType.Normal) { //普通方式验证 string[] source ={"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"}; for (int i = 0; i < CodeLength; i++) { sResult += source[rd.Next(0, source.Length)]; } System.Web.HttpContext.Current.Session["VerifyCode"] = sResult; } else { //运算符方式验证 int Num1 = rd.Next(10); int Num2 = rd.Next(10); int Result = 0; int F = (rd.Next(3) + 1); switch (F) { case 1: Result = Num1 + Num2; sResult=Num1+"+"+Num2+"="; break; case 2: Result = Num1 * Num2; sResult = Num1 + "×" + Num2 + "="; break; case 3: if (Num1 < Num2) { Result = Num2 - Num1; sResult = Num2 + "-" + Num1 + "="; } else { Result = Num1 - Num2; sResult = Num1 + "-" + Num2 + "="; } break; default: Result = Num1 + 9; sResult = Num1 + "+9 ="; break; } System.Web.HttpContext.Current.Session["VerifyCode"] = Result.ToString(); } return sResult; } #endregion } #region 验证方式 /// <summary> /// 验证方式。 /// Normal,普通数字+字母验证。 /// Operation,运算符验证。 /// </summary> public enum ValidateType { Normal = 1, Operation = 2, } #endregion }
调用方法。
TheCms.Common.Captcha _captcha = new TheCms.Common.Captcha(); _captcha.ValidateType = TheCms.Common.ValidateType.Operation; _captcha.CodeLength = 4;//如果在普通方式下有用 运算符方式没用。 _captcha.CreateCaptch();
验证方法:
if (TheCms.Common.Captcha.CheckVerifyCode(TextBox1.Text)) { Response.Write("对"); } else { Response.Write("错"); }
高手别喷 纯属交流。
运算符 偷懒鸟。只写了 + - *
相关文章推荐
- 汇编语言中有一种移位指令叫做循环左移(ROL),现在有个简单的任务,就是用字符串模拟这个指令的运算结果。对于一个给定的字符序列S,请你把其循环左移K位后的序列输出。例如,字符序列S=”abcXYZde
- 【Visual Basic】纯代码不拖控件,利用动态生成控件的方式完成一个简单的四则运算计算器
- 一个简单的PHP验证码和验证方法。
- 汇编语言中有一种移位指令叫做循环左移(ROL),现在有个简单的任务,就是用字符串模拟这个指令的运算结果。对于一个给定的字符序列S,请你把其循环左移K位后的序列输出。例如,字符序列S=”abcXYZdef”,要求输出循环左移3位后的结果,即“XYZdefabc”。是不是很简单?OK,搞定它!
- 汇编语言中有一种移位指令叫做循环左移(ROL),现在有个简单的任务,就是用字符串模拟这个指令的运算结果。对于一个给定的字符序列S,请你把其循环左移K位后的序列输出。例如,字符序列S=”abcXYZde
- 安全验证码,简单加减法运算,机器无法识别的验证码
- 如何给登录页面添加一个验证是否是机器登录的方式(验证码)
- 关于实现移动终端间互通信及相应交互方式的简单设想——畅想一个信息自由流动的时代
- jquery 做的一个简单切换图片和内容的效果
- 三层架构(MVC)实现简单登陆注册验证(含验证码)
- JMS P2P方式的消息发送/接收的一个简单例子
- 一个简单的网络验证程序的分析
- 写了一个简单的ajax操作类
- JavaScript一个简单的验证页面
- 使用redisTemplete简单封装的一个通用操作类
- 一个生成验证字符或数字的增强类(downmoon)(基于.net1.1)及调用示例
- 最近用RecurDyn做了一个简单的车辆四轮模型,验证软件能不能做整车仿真
- iOS实现一个简单的页面切换控件
- PHP生成一个简单的验证码图片
- jquery表单验证插件——使用方式非常简单,明了