本文实例报告了asp.net运用ashx生成图形考证码的要领。分享给人人供人人参考,细致以下:
考证码的优点不必我多说,你们都懂的。我在网上看到有人把考证码直接写在aspx页面里,也就是说这类体式格局要求考证码即是要求一个页面,如许做很不科学。以下所示
<form id="form1" runat="server"> <p> <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" /> </p> </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>
说了半天,该是时刻看看生成的考证码长什么样了
以上就是asp.net应用ashx完成考证码功用详解的细致内容,更多请关注ki4网别的相干文章!