博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Asp.Net验证码1
阅读量:7098 次
发布时间:2019-06-28

本文共 3130 字,大约阅读时间需要 10 分钟。

验证码html调用

验证码:验证码

验证码刷新

//刷新验证码        function refreshRandCode() {            $('#imgCode').hide().attr('src',            'CodeHandler.ashx?' + Math.floor(Math.random() * 100)).fadeIn();        }

验证码后台判断

Session["checkCode"]  

 还有2个验证码效果请看我下一篇博客

验证码CodeHandler.ashx

难以分清的的字母i、o已删除

<%@ WebHandler Language="C#" Class="CodeHandler" %>using System;using System.Web;using System.Drawing;public class CodeHandler : IHttpHandler, System.Web.SessionState.IRequiresSessionState{    public string charSet = "0,1,2,3,4,5,6,8,9,A,B,C,D,E,F,G,H,J,K,L,M,N,P,Q,R,S,T,U,V,W,X,Y,Z";    public void ProcessRequest (HttpContext context) {        string validateCode = CreateRandomCode(4);        context.Session["checkCode"] = validateCode;        CreateImage(validateCode, context);       }     public bool IsReusable {        get {            return false;        }    }    ///         /// 生成验证码              /// 位数       /// 
验证码字符串
private string CreateRandomCode(int n) { string[] CharArray = charSet.Split(','); string randomCode = ""; int temp = -1; Random rand = new Random(); for (int i = 0; i < n; i++) { if (temp != -1) { rand = new Random(i * temp * ((int)DateTime.Now.Ticks)); } int t = rand.Next(CharArray.Length - 1); if (temp == t) { return CreateRandomCode(n); } temp = t; randomCode += CharArray[t]; } return randomCode; } private void CreateImage(string checkCode, HttpContext context) { int iwidth = (int)(checkCode.Length * 14+10); System.Drawing.Bitmap image = new System.Drawing.Bitmap(iwidth, 23); Graphics g = Graphics.FromImage(image); Font f = new System.Drawing.Font("Arial", 14, (System.Drawing.FontStyle.Italic | System.Drawing.FontStyle.Bold)); // 前景色 Brush b = new System.Drawing.SolidBrush(Color.Black); // 背景色 g.Clear(Color.White); // 填充文字 g.DrawString(checkCode, f, b, 0, 1); // 随机线条 Pen linePen = new Pen(Color.Gray, 0); Random rand = new Random(); for (int i = 0; i < 10; i++) { int x1 = rand.Next(image.Width); int y1 = rand.Next(image.Height); int x2 = rand.Next(image.Width); int y2 = rand.Next(image.Height); g.DrawLine(linePen, x1, y1, x2, y2); } // 随机点 for (int i = 0; i < 50; i++) { int x = rand.Next(image.Width); int y = rand.Next(image.Height); image.SetPixel(x, y, Color.Gray); } // 边框 g.DrawRectangle(new Pen(Color.Gray), 0, 0, image.Width - 1, image.Height - 1); // 输出图片 System.IO.MemoryStream ms = new System.IO.MemoryStream(); image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); context.Response.ClearContent(); context.Response.ContentType = "image/Jpeg"; context.Response.BinaryWrite(ms.ToArray()); g.Dispose(); image.Dispose(); } }

 验证码效果:

转载地址:http://cfaql.baihongyu.com/

你可能感兴趣的文章
Eclipse+超快速的模拟器Genymotion开展Android申请书(第一步:安装和配置Genymotion)...
查看>>
MySQL查看一个表的创建文本以及删除表某列的索引
查看>>
BZOJ3009 : 集合
查看>>
android图片压缩的3种方法实例
查看>>
SVN(TortoiseSVN)提交时忽略bin跟obj目录
查看>>
tophat
查看>>
Codeforces Round #326 (Div. 2) A. Duff and Meat 水题
查看>>
最优二叉查找树
查看>>
Android屏幕适配全攻略(最权威的官方适配指导) (转)
查看>>
AutoMapper(七)
查看>>
内存数据库:memcached与redis技术的对比试验
查看>>
仿58同城UITableViewCell动画
查看>>
android service 的各种用法(IPC、AIDL)
查看>>
MongoDB聚合运算之mapReduce函数的使用(11)
查看>>
Android 屏蔽Power键 Home键
查看>>
python之装饰器
查看>>
泛型的基本介绍和使用
查看>>
新书《iOS8 Swift编程指南》货架
查看>>
Python与rrdtool的结合模块
查看>>
写贤治学生:关键是要管理好自己的时间
查看>>