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
您可能感兴趣的文章
- 01-14旧照修复-旧照修复应用软件功能介绍
- 04-15华为手机恢复原始图标方法
- 01-12捏脸-捏脸应用软件功能介绍
- 01-14万小能无线钥匙-万小能无线钥匙应用软
- 12-21OPPO手机怎么恢复已删除联系人
- 12-09iqoo8怎么设置影院音效
- 11-23OPPO手机怎么开启双时钟
- 10-25苹果13miniSafari长截屏教程介绍
- 03-25一加8t悬浮窗功能开启教程
- 01-12健康之翼-健康之翼应用软件功能介绍
- 02-18mongoDB 实现主从读写分离实现的实例
- 02-28如何查看MySql数据库物理文件存放位置
- 11-04华为平板合盖关屏在哪里设置
- 03-11正确设置nginx/php-fpm/apache权限 提
- 07-05Linux ftpcount命令
- 03-09鸿蒙桌面设置布局教程
- 10-04讯飞输入法密语模式怎么开
- 05-06in a frame because it set 'X-Frame-
- 09-27ipadmini分屏如何调整大小
- 12-26必连5G大师-必连5G大师应用软件功能介
最近更新
阅读排行
猜你喜欢
- 04-02小米10s将画面投屏到电视操作方法
- 02-11三星s10截屏方法汇总
- 01-11万能水印打卡相机-万能水印打卡相机应
- 03-19红魔6触感功能关闭教程
- 07-15帝国uft8编码结合项的问题
- 10-14小米10自动接听电话在哪设置
- 12-29临床执业医师题库-临床执业医师题库应
- 11-18天猫精灵方糖2如何绑定电视
- 01-20红米手机自动锁屏时长怎么设置
- 01-12捏脸-捏脸应用软件功能介绍