这里用的一般处理程序画的验证码图片。
判断验证码步骤:
①先在一般处理程序中获取验证码(一般处理程序中session的调用→context.Session[])
string code = GetRndStr(); //将验证码存入session中 context.Session["yzm"] = code;
②在一般处理程序中使用session必须实现一个接口,接口的命名空间为using System.Web.SessionState;
接口名:IRequiresSessionState
③再判断验证码时要以防万一,,session的生命周期,所以先判断Session是否为空,不是空再继续以后的判断
ToLower()或者ToUpper()可以使验证码大小写不敏感
//一,先判断验证码 if (txtYZM.Text.Trim().ToLower() == Session["yzm"].ToString().ToLower() && Session["yzm"]!=null) { //判断验证码成功后,删除session Session.Remove("yzm"); //二,再判断账号密码
一般处理程序中的验证码
using System.Web.SessionState; public class ValidateCode : IHttpHandler,IRequiresSessionState { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "image/jpeg"; string code = GetRndStr(); //将验证码存入session中 context.Session["yzm"] = code; using (Bitmap img = CreateImages(code, "ch")) { img.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg); } } public bool IsReusable { get { return false; } } /// <summary> /// 数字随机数 /// </summary> /// <returns></returns> private string GetRndNum() { string code = string.Empty; Random random = new Random(); ; i < ; i++) { code += random.Next(); } return code; } /// <summary> /// 英文随机 /// </summary> /// <returns></returns> private string GetRndStr() { 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"; string[] VcArray = Vchar.Split(','); string checkCode = string.Empty; Random rand = new Random(); ; i < ; i++) { int t = rand.Next(VcArray.Length); checkCode += VcArray[t]; } return checkCode; } /// <summary> /// 中文随机 /// </summary> /// <returns></returns> private string GetRndCh() { System.Text.Encoding gb = System.Text.Encoding.Default;//获取GB2312编码页(表) );//生4个随机中文汉字编码 ]; System.Text.StringBuilder sb = new System.Text.StringBuilder(); ; i < ; i++) { //根据汉字编码的字节数组解码出中文汉字 str[i] = gb.GetString((byte[])Convert.ChangeType(bytes[i], typeof(byte[]))); sb.Append(str[i].ToString()); } return sb.ToString(); } /// <summary> /// 产生随机中文字符 /// </summary> /// <param name="strlength"></param> /// <returns></returns> private static object[] CreateRegionCode(int strlength) { //定义一个字符串数组储存汉字编码的组成元素 ] { ", "a", "b", "c", "d", "e", "f" }; Random rnd = new Random(); object[] bytes = new object[strlength]; ; i < strlength; i++) { //区位码第1位 , ); string str_r1 = rBase[r1].Trim(); //区位码第2位 rnd = new Random(r1 * unchecked((int)DateTime.Now.Ticks) + i); int r2; ) { r2 = rnd.Next(, ); } else { r2 = rnd.Next(, ); } string str_r2 = rBase[r2].Trim(); //区位码第3位 rnd = new Random(r2 * unchecked((int)DateTime.Now.Ticks) + i);//更换随机种子 , ); string str_r3 = rBase[r3].Trim(); //区位码第4位 rnd = new Random(r3 * unchecked((int)DateTime.Now.Ticks) + i); int r4; ) { r4 = rnd.Next(, ); } ) { r4 = rnd.Next(, ); } else { r4 = rnd.Next(, ); } string str_r4 = rBase[r4].Trim(); //定义两个字节变量存储产生的随机汉字区位码 ); ); //将两个字节变量存储在字节数组中 byte[] str_r = new byte[] { byte1, byte2 }; //将产生的一个汉字的字节数组放入object数组中 bytes.SetValue(str_r, i); } return bytes; } /// <summary> /// 画图片的背景图+干扰线 /// </summary> /// <param name="checkCode"></param> /// <returns></returns> private Bitmap CreateImages(string checkCode, string type) { ; if (type == "ch") { step = ;//中文字符,边界值做大 } + step)); System.Drawing.Bitmap image = ); Graphics g = Graphics.FromImage(image); g.Clear(Color.White);//清除背景色 Color[] c = { Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Orange, Color.Brown, Color.DarkCyan, Color.Purple };//定义随机颜色 string[] font = { "Verdana", "Microsoft Sans Serif", "Comic Sans MS", "Arial", "宋体" }; Random rand = new Random(); ; i < ; 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(), x1, y1, x2, y2);//根据坐标画线 } ; i < checkCode.Length; i++) { ); ); Font f = , System.Drawing.FontStyle.Bold); Brush b = new System.Drawing.SolidBrush(c[cindex]); ; ) % == ) { ii = ; } g.DrawString(checkCode.Substring(i, ), f, b, + (i * ( + step)), ii); } g.DrawRectangle(), , , image.Width - , image.Height - ); System.IO.MemoryStream ms = new System.IO.MemoryStream(); return image; } }