使用HttpHandler输出验证码
2010-06-24 11:58
393 查看
C#生成随机验证码
PS:
验证码给用户带来的不友好体验已经越来越明显了。
多数网站也都已经开始尝试避免验证码的输入,比如:淘宝的登录,只有在多次登录错误的情况下,才会出现验证码。
眼瞅着验证码已经开始向幕后转移了,验证码会以这种方式落幕吗?
验证码的明天会是怎样呢?
欢迎留言~~
PS:
验证码给用户带来的不友好体验已经越来越明显了。
多数网站也都已经开始尝试避免验证码的输入,比如:淘宝的登录,只有在多次登录错误的情况下,才会出现验证码。
眼瞅着验证码已经开始向幕后转移了,验证码会以这种方式落幕吗?
验证码的明天会是怎样呢?
欢迎留言~~
/// <summary> /// 反回验证码图片,并保存文字于SESSION /// </summary> [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] public class ValiCode : IHttpHandler, IRequiresSessionState { private const string _CODEITEM = "qwertyuioplkjhgfdsazxcvbnmQWERTYUIOPLKJHGFDSAZXCVBNM123456789"; private const string _SAVESESSIONNAME = "ValiCode"; private readonly Random _Random = new Random(); // 可选字体 private readonly string[] _FamilyName = new string[] { "Arial", "Helvetica", "Geneva", "sans-serif", "Verdana" }; // 可选颜色 private readonly string[] _BrushName = new string[] { "OliveDrab", "ForestGreen", "DarkCyan", "LightSlateGray", "RoyalBlue", "SlateBlue", "DarkViolet", "MediumVioletRed", "IndianRed", "Firebrick", "Chocolate", "Peru", "Goldenrod" }; // 可选笔刷 private readonly Brush[] _BrushItems = new Brush[] { Brushes.OliveDrab, Brushes.ForestGreen, Brushes.DarkCyan, Brushes.LightSlateGray, Brushes.RoyalBlue, Brushes.SlateBlue, Brushes.DarkViolet, Brushes.MediumVioletRed, Brushes.IndianRed, Brushes.Firebrick, Brushes.Chocolate, Brushes.Peru, Brushes.Goldenrod }; // 可选字大小(面积) private readonly int[] _FontSize = new int[] { 13, 14, 15, 16, 17, 18, 19, 20 }; // 开始绘制时X坐标位置 private int _NowPointX = 0; // 开始绘制时Y坐标位置 private int _NowPointY = 0; // 图片模糊点数量 private int _PixelCount = 100; // 图片贝塞尔样条数量 private int _BezierCount = 2; public void ProcessRequest(HttpContext context) { string code = GetRandomCode(4); Bitmap map = GetRandomBitmap(80, 25, code); map.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg); // 将图片对象保存到输出流 context.Session[_SAVESESSIONNAME] = code; // 将验证文字保存于Session context.Response.End(); } // 生成验证码 private Bitmap GetRandomBitmap(int width, int height, string codeStr) { if (width < 1 || height < 1 || string.IsNullOrEmpty(codeStr)) { return null; } Bitmap map = new Bitmap(width, height); // 创建图片 Graphics grap = Graphics.FromImage(map); // 创建画板 grap.Clear(Color.White); // 图片默认填充色 char[] code = codeStr.ToArray(); // 向画板中绘制文字 PointF point; foreach (char citem in code) { point = new PointF(_NowPointX, _NowPointY); grap.DrawString(citem.ToString(), GetRandomFont(), GetRandomBrush(), point); } // 向画板中绘制贝塞尔样条 int doInt = 0; do { grap.DrawBezier(GetRandomPen(), GetRandomPoint(width, height), GetRandomPoint(width, height), GetRandomPoint(width, height), GetRandomPoint(width, height)); doInt++; } while (doInt < _BezierCount); grap.Dispose(); // 在图片中添加模糊点 doInt = 0; do { map.SetPixel(_Random.Next(width), _Random.Next(height), Color.FromName(_BrushName[_Random.Next(0, _BrushName.Length - 1)])); doInt++; } while (doInt < _PixelCount); return map; } // 在固定范围内返回随机坐标 private Point GetRandomPoint(int width, int height) { return new Point(_Random.Next(width), _Random.Next(height)); } // 从填充对象数组中返回随机填充对象 private Brush GetRandomBrush() { return _BrushItems[_Random.Next(0, _BrushItems.Length - 1)]; } // 从字体数组中返回随机字体 private Font GetRandomFont() { int size = _FontSize[_Random.Next(0, _FontSize.Length - 1)]; Font font = new Font(_FamilyName[_Random.Next(0, _FamilyName.Length - 1)], size); _NowPointX += size; return font; } // 返回随机画笔对象 private Pen GetRandomPen() { return new Pen(_BrushItems[_Random.Next(0, _BrushItems.Length - 1)]); } // 返回验证码字符串 private string GetRandomCode(int len) { if (len < 1) { return null; } StringBuilder randomStr = new StringBuilder(); char[] source = _CODEITEM.ToCharArray(); int beg = 0; int end = source.Length - 1; for (int i = 0; i < len; i++) { randomStr.Append(source[_Random.Next(beg, end)]); // 随机从字符数组中获取字符添加到StringBuilder对象中 } return randomStr.ToString(); } public bool IsReusable { get { return true; } } }
相关文章推荐
- HttpHandler使用Session
- c# 使用 HttpWebRequest模拟登陆(附带验证码)
- 使用jQuery+HttpHandler+xml模拟一个三级联动的例子
- 在窗口应用中使用printf,cout 等将输出重定向到console收藏 http://blog.csdn.net/royer/archive/2007/01/25/1492968.aspx
- asp.net 请求输入到输出的全过程 httpHandler和httpModuler
- 【转】使用HttpHandler为图片添加水印
- Flex与.NET互操作(五):使用FileReference+HttpHandler实现文件上传/下载
- 在自定义HttpHandler中无法使用Session[zt]
- 使用HttpHandler做文件过滤器,验证下载文件权限
- MSDN: 在 ASP.NET 中使用 HTTPHandler 实现 Front Controller
- Flex与.NET互操作(五):使用FileReference+HttpHandler实现文件上传/下载
- [ 转]c# 使用 HttpWebRequest模拟登陆(附带验证码)
- c# 使用 HttpWebRequest模拟登陆(附带验证码)
- 使用HttpTextWriter类输出
- 使用HttpHandler出现的无法使用Session的错误及解决
- asp.net使用httphandler打包多CSS或JS文件以加快页面加载速度
- HttpHandler全站过滤替换Html输出
- HttpModule,HttpHandler,HttpHandlerFactory简单使用
- asp.net 使用HttpModule对全站输出的动态页面的HTML内容进行修改,不会错乱
- [已上传demo]使用HttpHandler做文件过滤器,验证下载文件权限