这里用的一般处理程序画的验证码图片。

判断验证码步骤:

①先在一般处理程序中获取验证码(一般处理程序中session的调用→context.Session[])

  1. string code = GetRndStr();
  2. //将验证码存入session中
  3. context.Session["yzm"] = code;

②在一般处理程序中使用session必须实现一个接口,接口的命名空间为using System.Web.SessionState;

接口名:IRequiresSessionState

③再判断验证码时要以防万一,,session的生命周期,所以先判断Session是否为空,不是空再继续以后的判断

  1. ToLower()或者ToUpper()可以使验证码大小写不敏感
  1. //一,先判断验证码
  2. if (txtYZM.Text.Trim().ToLower() == Session["yzm"].ToString().ToLower() && Session["yzm"]!=null)
  3. {
  4. //判断验证码成功后,删除session
  5. Session.Remove("yzm");
  6. //二,再判断账号密码

一般处理程序中的验证码

  1. using System.Web.SessionState;
  2. public class ValidateCode : IHttpHandler,IRequiresSessionState {
  3.  
  4. public void ProcessRequest(HttpContext context)
  5. {
  6. context.Response.ContentType = "image/jpeg";
  7. string code = GetRndStr();
  8. //将验证码存入session中
  9. context.Session["yzm"] = code;
  10. using (Bitmap img = CreateImages(code, "ch"))
  11. {
  12. img.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
  13. }
  14. }
  15.  
  16. public bool IsReusable
  17. {
  18. get
  19. {
  20. return false;
  21. }
  22. }
  23. /// <summary>
  24. /// 数字随机数
  25. /// </summary>
  26. /// <returns></returns>
  27. private string GetRndNum()
  28. {
  29. string code = string.Empty;
  30. Random random = new Random();
  31. ; i < ; i++)
  32. {
  33. code += random.Next();
  34. }
  35. return code;
  36. }
  37. /// <summary>
  38. /// 英文随机
  39. /// </summary>
  40. /// <returns></returns>
  41. private string GetRndStr()
  42. {
  43. string Vchar = "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";
  44. string[] VcArray = Vchar.Split(',');
  45. string checkCode = string.Empty;
  46. Random rand = new Random();
  47. ; i < ; i++)
  48. {
  49. int t = rand.Next(VcArray.Length);
  50. checkCode += VcArray[t];
  51. }
  52. return checkCode;
  53. }
  54. /// <summary>
  55. /// 中文随机
  56. /// </summary>
  57. /// <returns></returns>
  58. private string GetRndCh()
  59. {
  60. System.Text.Encoding gb = System.Text.Encoding.Default;//获取GB2312编码页(表)
  61. );//生4个随机中文汉字编码
  62. ];
  63. System.Text.StringBuilder sb = new System.Text.StringBuilder();
  64. ; i < ; i++)
  65. {
  66. //根据汉字编码的字节数组解码出中文汉字
  67. str[i] = gb.GetString((byte[])Convert.ChangeType(bytes[i], typeof(byte[])));
  68. sb.Append(str[i].ToString());
  69. }
  70. return sb.ToString();
  71. }
  72. /// <summary>
  73. /// 产生随机中文字符
  74. /// </summary>
  75. /// <param name="strlength"></param>
  76. /// <returns></returns>
  77. private static object[] CreateRegionCode(int strlength)
  78. {
  79. //定义一个字符串数组储存汉字编码的组成元素
  80. ] { ", "a", "b", "c", "d", "e", "f" };
  81. Random rnd = new Random();
  82. object[] bytes = new object[strlength];
  83.  
  84. ; i < strlength; i++)
  85. {
  86. //区位码第1位
  87. , );
  88. string str_r1 = rBase[r1].Trim();
  89. //区位码第2位
  90. rnd = new Random(r1 * unchecked((int)DateTime.Now.Ticks) + i);
  91. int r2;
  92. )
  93. {
  94. r2 = rnd.Next(, );
  95. }
  96. else
  97. {
  98. r2 = rnd.Next(, );
  99. }
  100. string str_r2 = rBase[r2].Trim();
  101.  
  102. //区位码第3位
  103. rnd = new Random(r2 * unchecked((int)DateTime.Now.Ticks) + i);//更换随机种子
  104. , );
  105. string str_r3 = rBase[r3].Trim();
  106.  
  107. //区位码第4位
  108. rnd = new Random(r3 * unchecked((int)DateTime.Now.Ticks) + i);
  109. int r4;
  110. )
  111. {
  112. r4 = rnd.Next(, );
  113. }
  114. )
  115. {
  116. r4 = rnd.Next(, );
  117. }
  118. else
  119. {
  120. r4 = rnd.Next(, );
  121. }
  122. string str_r4 = rBase[r4].Trim();
  123. //定义两个字节变量存储产生的随机汉字区位码
  124. );
  125. );
  126.  
  127. //将两个字节变量存储在字节数组中
  128. byte[] str_r = new byte[] { byte1, byte2 };
  129.  
  130. //将产生的一个汉字的字节数组放入object数组中
  131. bytes.SetValue(str_r, i);
  132. }
  133. return bytes;
  134. }
  135. /// <summary>
  136. /// 画图片的背景图+干扰线
  137. /// </summary>
  138. /// <param name="checkCode"></param>
  139. /// <returns></returns>
  140. private Bitmap CreateImages(string checkCode, string type)
  141. {
  142. ;
  143. if (type == "ch")
  144. {
  145. step = ;//中文字符,边界值做大
  146. }
  147. + step));
  148. System.Drawing.Bitmap image = );
  149. Graphics g = Graphics.FromImage(image);
  150. g.Clear(Color.White);//清除背景色
  151. Color[] c = { Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Orange, Color.Brown, Color.DarkCyan, Color.Purple };//定义随机颜色
  152. string[] font = { "Verdana", "Microsoft Sans Serif", "Comic Sans MS", "Arial", "宋体" };
  153. Random rand = new Random();
  154.  
  155. ; i < ; i++)
  156. {
  157. int x1 = rand.Next(image.Width);
  158. int x2 = rand.Next(image.Width);
  159. int y1 = rand.Next(image.Height);
  160. int y2 = rand.Next(image.Height);
  161. g.DrawLine(), x1, y1, x2, y2);//根据坐标画线
  162. }
  163.  
  164. ; i < checkCode.Length; i++)
  165. {
  166. );
  167. );
  168.  
  169. Font f = , System.Drawing.FontStyle.Bold);
  170. Brush b = new System.Drawing.SolidBrush(c[cindex]);
  171. ;
  172. ) % == )
  173. {
  174. ii = ;
  175. }
  176. g.DrawString(checkCode.Substring(i, ), f, b, + (i * ( + step)), ii);
  177.  
  178. }
  179. g.DrawRectangle(), , , image.Width - , image.Height - );
  180. System.IO.MemoryStream ms = new System.IO.MemoryStream();
  181. return image;
  182. }
  183. }

winform之判断验证码,,附加验证码的一般处理程序的更多相关文章

  1. C# WinForm 使用SMS接口发送手机验证码+图形验证码+IP限制

    https://blog.csdn.net/IT_xiao_guang_guang/article/details/104299983 前言   1.发送手机验证码用的是网建的SMS接口(http:/ ...

  2. Js 判断输入的验证码是否一致

    实现效果: 判断输入的验证码是否一致 如果不同,alert出验证码输入有误~, 输入正确输出登录成功. <!DOCTYPE html> <html lang="en&quo ...

  3. 简单说说Spring Security 使用(附加验证码登录,自定义认证)

    先看官方文档:http://docs.spring.io/spring-security/site/docs/4.0.x/reference/htmlsingle/ spring security4已 ...

  4. c#winform图片绘制与图片验证码

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  5. 《新手养成记》--第一篇 iOS手机号正则判断和获取验证码

    今天是开通一个新博客开启新的一年,同时也是对自己的过去做一个告别.介绍一下博主自己,资深iOS屌丝男,今天写这个博客就是刻意拉低逼格的,前两年写的博客为了装那什么,故意写的高大上,其实呵呵哒...年假 ...

  6. java验证码-汉字验证码

    今天整理了一个java实现的汉字输入验证码 主要包含两个类,一个是生成验证码,一个是判断验证码输入是否正确 实现原理非常简单,将汉字和干扰线生成图片并将汉字保存到session,前台获取每次生成验证码 ...

  7. C# Ajax 手机发送短信验证码 校验验证码 菜鸟级别实现方法

    1.Ajax请求处理页面: using System; using System.Collections.Generic; using System.Linq; using System.Web; u ...

  8. php生成动态验证码 加减算法验证码 简单验证码

    预览效果: <?php /** *ImageCode 生成包含验证码的GIF图片的函数 *@param $string 字符串 *@param $width 宽度 *@param $height ...

  9. Java Swing 图形界面实现验证码(验证码可动态刷新)

    import java.awt.Color;import java.awt.Font;import java.awt.Graphics;import java.awt.Toolkit;import j ...

随机推荐

  1. 对"构建之法“的理解和困惑

    对"构建之法"的理解和困惑        本人"学沫沫"一个,对于之前的编程学习虽不大"感冒",但秉着对自己负责的态度进行了基础学习.   ...

  2. gunplot demo

    //author : Leon yangli0534@gmail.com #include <stdlib.h> #include <stdio.h> #include < ...

  3. document.write和innerHTML的区别

    document.write是直接写入到页面的内容流,如果在写之前没有调用document.open, 浏览器会自动调用open.每次写完关闭之后重新调用该函数,会导致页面被重写. innerHTML ...

  4. tomcat配置jenkins遇到的问题

    在执行jenkinks时,遇到以下错误: 原因:未在tomcat/conf中的tomcat-users.xml中配置用户 解决方法:在tomcat/conf/tomcat-users.xml中添加以下 ...

  5. Linux下php安装memcache扩展

    安装环境:CentOS 6.4 php扩展memcache的作用是为了支持memcached数据库缓存服务器,下面是安装方法. 1.下载 下载地址:http://pecl.php.net/packag ...

  6. 11Spring_AOP编程(AspectJ)_概述

    AspectJ 是一个框架 (第三方AOP框架 ),提供切面编程 ,编写一个Aspect 支持多个Advice和多个PointCut .对比前一种提到的传统的Aop编程,AspctJ更加的常用.Asp ...

  7. 解决 php-cgi 启动时提示缺少 msvcr110.dll 的问题

    问题描述: 启动 php-cgi 时如果提示缺少 msvcr110.dll 问题原因: 缺少 vc 2012 运行库   问题解决: 需要安装 vcredist_x64.exe 或 vcredist_ ...

  8. 转载:有关SQL server connection KeepAlive 的FAQ

    转:http://blogs.msdn.com/b/apgcdsd/archive/2011/05/03/sql-server-connection-keepalive-faq.aspx 1.什么是S ...

  9. 什么是json

    http://www.ruanyifeng.com/blog/2009/05/data_types_and_json.html http://edu.51cto.com/lesson/id-71123 ...

  10. CentOS7 SSH相关

    1.查看SSH是否已经安装,命令: rpm -qa |grep ssh 如果列出了openssh-x.x开头的软件名,代表已经安装 如果没有安装,使用命令安装: yum install ssh 2.如 ...