asp.net使用ashx生成图形验证码的方法示例
本文实例讲述了asp.net使用ashx生成图形验证码的方法。分享给大家供大家参考,具体如下:
验证码的好处不用我多说,你们都懂的。我在网上看到有人把验证码直接写在aspx页面里,也就是说这种方式请求验证码等于请求一个页面,这样做很不科学。如下所示
<form id="form1" runat="server"> <div> <asp:Image ID="Image1" runat="server" ImageUrl="Default.aspx" /> <br /> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" /> </div> </form>
这个代码看着就觉得写代码的人比较欠揍,代码写成这样子着实郁闷。验证吗也不写点script做下切换。
下面我介绍一种方式来实现这样的功能
1. 写个ashx生成图形验证码
using System; using System.Collections; using System.Data; using System.Linq; using System.Web; using System.Web.Services; using System.Web.Services.Protocols; using System.Xml.Linq; using System.Web.SessionState; using System.Drawing; namespace usechecknum.ashx { /// <summary> /// $codebehindclassname$ 的摘要说明 /// </summary> [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] public class doCreateNum : IHttpHandler,IRequiresSessionState { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/html"; string checkCode = GetValidation(5); // 产生5位随机验证码字符 context.Session["Code"] = checkCode; //将字符串保存到Session中,以便需要时进行验证 System.Drawing.Bitmap image = new System.Drawing.Bitmap(70, 22); Graphics g = Graphics.FromImage(image); try { //生成随机生成器 Random random = new Random(); //清空图片背景色 g.Clear(Color.White); // 画图片的背景噪音线 int i; for (i = 0; i < 25; i++) { int x1 = random.Next(image.Width); int x2 = random.Next(image.Width); int y1 = random.Next(image.Height); int y2 = random.Next(image.Height); g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2); } Font font = new System.Drawing.Font("Arial", 12, (System.Drawing.FontStyle.Bold)); System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2F, true); g.DrawString(checkCode, font, brush, 2, 2); //画图片的前景噪音点 g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1); System.IO.MemoryStream ms = new System.IO.MemoryStream(); image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif); context.Response.ClearContent(); context.Response.ContentType = "image/Gif"; context.Response.BinaryWrite(ms.ToArray()); } finally { g.Dispose(); image.Dispose(); } } public string GetValidation(int num) { string str = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; //"或者写汉字也行" string validatecode = ""; Random rd = new Random(); for (int i = 0; i < num; i++) { validatecode += str.Substring(rd.Next(0, str.Length), 1); } return validatecode; } public bool IsReusable { get { return false; } } } }
2. 在页面上显示验证码,因为我们生成的是图形,所以可以直接写在<img/>标签里,我们只要写段简单的脚本就可以点击鼠标切换验证码了
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="index.aspx.cs" Inherits="usechecknum._Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>验证码的使用</title> </head> <script language="javascript" type="text/javascript"> function changeCode() { var imgNode = document.getElementById("vimg"); imgNode.src = "ashx/doCreateNum.ashx?t=" + (new Date()).valueOf(); // 这里加个时间的参数是为了防止浏览器缓存的问题 } </script> <body> <form id="form1" runat="server"> 请输入验证码:<input type="text" name="checknum"/><img src="ashx/doCreateNum.ashx" id="vimg" onclick="changeCode()" /> </form> </body> </html>
说了半天,该是时候看看生成的验证码长什么样了
(资源库 www.zyku.net)
栏 目:ASP.NET
下一篇:无法向会话状态服务器发出会话状态请求。请确保 ASP.NET State Service (ASP.NET 状态服务)已启动,并且客户端端口与服务器端口相同
本文标题:asp.net使用ashx生成图形验证码的方法示例
本文地址:https://www.zyku.net/aspnet/1497.html
您可能感兴趣的文章
- 09-22荣耀50如何截长图
- 09-19iqoo8pro如何更换锁屏桌面
- 02-26帝国CMS插件二次开发格式建议
- 10-15华为手机添加一键锁屏插件方法介绍
- 10-01抖音上线音量过高提醒功能介绍
- 10-13华为日历显示节假日休假信息
- 03-31PyPDF2的使用
- 08-14荣耀50nfc功能在哪里打开
- 02-19WordPress文章摘要自动截取方案
- 10-07QQ音乐切换大字模式方法介绍
- 02-23DedeCMS内容页调用当前栏目名称及链接
- 09-17vivox70怎么截图
- 03-10网站301跳转(重定向)正确操作方法
- 09-11华为手机创建健康码快捷指令教程介绍
- 02-21DedeCMS隐藏模板路径防止模板被盗的几
- 10-12realmegtneo2广告怎么关
- 06-20帝国网站管理系统(EmpireCMS)使用手册
- 12-04微信朋友圈封面视频怎么弄
- 12-23HTML <audio> 标签
- 04-05帝国CMS tag伪静态后列表页分页样式修

最近更新
阅读排行
猜你喜欢
- 07-16帝国CMS 7.5制作小说网站流程总结
- 02-17ASP.NET Core中使用默认MVC路由的配置
- 02-21Linux中vi文件保存退出命令“:wq"和":x
- 09-19三星watch4启动息屏提醒步骤介绍
- 10-27网易云音乐怎么共享歌单给好友
- 10-08一加9rt怎么开启面部解锁
- 11-04iwatch7如何设置语言
- 09-26支付宝查看疫苗接种记录方法介绍
- 12-31未来生活+-未来生活+应用软件功能介绍
- 03-04wordpress不使用插件实现分页导航教程