首页 / ASP.NET / Asp.Net验证码
Asp.Net验证码
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Asp.Net验证码,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2816字,纯文字阅读大概需要5分钟。
内容图文
![Asp.Net验证码](/upload/InfoBanner/zyjiaocheng/1183/3862e823f8a94de8bbcd936cd88754fe.jpg)
验证码html调用
验证码:<input name="txtCode0" type="text" id="txtCode0"class="login-text" size="10" maxlength="4"> <img src="CodeHandler.ashx" id="imgCode" alt="验证码" title="点击刷新验证码" align="middle" style="height: 24px; width: 70px;" onclick="refreshRandCode()">
验证码刷新
// 刷新验证码 function refreshRandCode() { $(‘#imgCode‘).hide().attr(‘src‘, ‘CodeHandler.ashx?‘ + Math.floor(Math.random() * 100)).fadeIn(); }
验证码后台判断
Session["checkCode"]
验证码CodeHandler.ashx
难以分清的的字母i、o已删除
<%@ WebHandler Language="C#" Class="CodeHandler" %> using System; using System.Web; using System.Drawing; publicclass CodeHandler : IHttpHandler, System.Web.SessionState.IRequiresSessionState{ publicstring 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"; publicvoid ProcessRequest (HttpContext context) { string validateCode = CreateRandomCode(4); context.Session["checkCode"] = validateCode; CreateImage(validateCode, context); } publicbool IsReusable { get { returnfalse; } } ///<summary>/// 生成验证码 ///<param name="n">位数</param>///<returns>验证码字符串</returns>privatestring 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; } privatevoid 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://www.cnblogs.com/webapi/p/5725576.html
内容总结
以上是互联网集市为您收集整理的Asp.Net验证码全部内容,希望文章能够帮你解决Asp.Net验证码所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。