asp.net写验证码
生成验证码与匹配验证码的服务端代码
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<%@ WebHandler Language="C#" Class="ValidataeCodeHandler" %> using System; using System.Web; using System.Drawing; using System.Drawing.Drawing2D; using System.IO; using System.Drawing.Imaging; public class ValidataeCodeHandler : IHttpHandler, System.Web.SessionState.IRequiresSessionState { public void ProcessRequest(HttpContext context) { //请求类型:获取验证码图片,匹配验证码 string type = context.Request["type"]; if (type == "math") { if (string.IsNullOrEmpty(context.Request["code"])) context.Response.Write(2); else if (string.IsNullOrEmpty("" + context.Session["yqcode" + context.Request["id"]])) { context.Response.Write(3); } else { if ("" + context.Session["yqcode" + context.Request["id"]] == context.Request["code"] + "") context.Response.Write(1); else context.Response.Write(0); } } else { context.Response.ContentType = "image/gif"; string validateCode = CreateValidateCode(context);//生成验证码 Bitmap bitmap = new Bitmap(imgWidth, imgHeight);//生成Bitmap图像 DisturbBitmap(bitmap); //图像背景 DrewValidateCode(bitmap, validateCode);//绘制验证码图像 bitmap.Save(context.Response.OutputStream, ImageFormat.Gif);//保存图像,等待输出 context.Response.Write(bitmap); } } // private int codeLen = 4;//验证码长度 private int fineness = 85;//图片清晰度 private int imgWidth = 48;//图片宽度 private int imgHeight = 24;//图片高度 private string fontFamily = "Times New Roman";//字体名称 private int fontSize = 14;//字体大小 //private int fontStyle = 0;//字体样式 private int posX = 0;//绘制起始坐标X private int posY = 0;//绘制坐标Y private string CreateValidateCode(HttpContext context) //生成验证码 { string validateCode = ""; Random random = new Random();// 随机数对象 validateCode = random.Next(1000, 9999) + ""; //for (int i = 0; i < codeLen; i++)//循环生成每位数值 //{ // int n = random.Next(10);//数字 // validateCode += n.ToString(); //} context.Session["yqcode" + context.Request["id"]] = validateCode;//保存验证码 这Session是在前台调用的。 return validateCode;// 返回验证码 } private void DisturbBitmap(Bitmap bitmap)//图像背景 { Random random = new Random();//通过随机数生成 for (int i = 0; i < bitmap.Width; i++)//通过循环嵌套,逐个像素点生成 { for (int j = 0; j < bitmap.Height; j++) { if (random.Next(90) <= this.fineness) bitmap.SetPixel(i, j, Color.LightGray); } } } private void DrewValidateCode(Bitmap bitmap, string validateCode)//绘制验证码图像 { Graphics g = Graphics.FromImage(bitmap);//获取绘制器对象 Font font = new Font(fontFamily, fontSize, FontStyle.Bold);//设置绘制字体 g.DrawString(validateCode, font, Brushes.Black, posX, posY);//绘制验证码图像 } public bool IsReusable { get { return false; } } }
手动刷新验证码
<script type="text/javascript"> //点击刷新验证码 function f_refreshtype() { var Image1 = document.getElementById("valiCode"); if (Image1 != null) { Image1.src = Image1.src + "?"; } } </script>
提交表单前ajax同步验证验证码是否正确
var urlCode = '/Handler/ValidataeCodeHandler.ashx?id=12&type=math&code=' + $.trim($("#yzcode").val()); //ajax同步请求 var mathresult = $.ajax({ type: "GET", url: urlCode, async: false }).responseText; if (mathresult != 1) { var Image1 = document.getElementById("valiCode"); if (Image1 != null) { Image1.src = Image1.src + "?"; } alert("验证码不匹配!"); return false; }
<div class="line"> <span>验证码:</span><input value="" type="text" name="yzcode" id="yzcode" class="tong" /> <em><img id="valiCode" name="valiCode" onclick="f_refreshtype();" style="height:32px;" src="/Handler/ValidataeCodeHandler.ashx?id=12"/></em> </div>