利用HttpHandler实现验证码

span style="font-size: 12pt">输出图片并保存验证码在Session里面。

注意:
必须继承System.Web.SessionState.IRequiresSessionState接口,才能实现Session读写!

 System.Web.SessionState的一些接口

 IReadOnlySessionState 指定目标 HTTP 处理程序只需要具有对会话状态值的读访问权限。这是一个标记接口,没有任何方法。
 IRequiresSessionState 指定目标 HTTP 处理程序需要对会话状态值具有读写访问权。这是一个标记接口,没有任何方法。

ValidateCode.ashx
  1<%@ WebHandler Language="C#" Class="ValidateCode" %>
  2
  3using System;
  4using System.Web;
  5using System.Web.SessionState;
  6
  7/**//// <summary>
  8/// 验证码程序
  9/// </summary>

 10public class ValidateCode : IHttpHandler, IRequiresSessionState
 11{
 12    /**//// <summary>
 13    /// 入口
 14    /// </summary>
 15    /// <param name="context"></param>

 16    public void ProcessRequest(HttpContext context)
 17    {
 18        string checkCode = CreateRandomCode(4);
 19        context.Session["Code"= checkCode;
 20        CreateImage(checkCode, context);
 21    }

 22
 23    public bool IsReusable
 24    {
 25        get
 26        {
 27            return false;
 28        }

 29    }

 30
 31    /**//// <summary>
 32    /// 产生验证码
 33    /// </summary>
 34    /// <param name="codeCount"></param>
 35    /// <returns></returns>

 36    private string CreateRandomCode(int codeCount)
 37    {
 38        //验证码中的出现的字符,避免了一些容易混淆的字符。
 39        string allChar = "3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,J,K,M,N,P,Q,R,S,T,U,W,X,Y";
 40        string[] allCharArray = allChar.Split(',');
 41        string randomCode = "";
 42        int temp = -1;
 43
 44        Random rand = new Random();
 45        for (int i = 0; i < codeCount; i++)
 46        {
 47            if (temp != -1)
 48            {
 49                rand = new Random(i * temp * ((int)DateTime.Now.Ticks));
 50            }

 51            int t = rand.Next(allCharArray.Length);
 52            if (temp == t)
 53            {
 54                return CreateRandomCode(codeCount);
 55            }

 56            temp = t;
 57            randomCode += allCharArray[t];
 58        }

 59        return randomCode;
 60    }

 61
 62    /**//// <summary>
 63    /// 创建图片
 64    /// </summary>
 65    /// <param name="checkCode"></param>
 66    /// <param name="context"></param>

 67    private void CreateImage(string checkCode, HttpContext context)
 68    {
 69        int iwidth = (int)(checkCode.Length * 12);
 70        System.Drawing.Bitmap image = new System.Drawing.Bitmap(iwidth, 20);
 71        System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image);
 72        g.Clear(System.Drawing.Color.White);
 73        //定义颜色
 74        System.Drawing.Color[] c = { System.Drawing.Color.DimGray, System.Drawing.Color.DimGray, System.Drawing.Color.DimGray };
 75        //定义字体            
 76        string[] font = "Verdana""Microsoft Sans Serif""Comic Sans MS""Arial""宋体" };
 77        Random rand = new Random();
 78        //随机输出噪点
 79        for (int i = 0; i < 50; i++)
 80        {
 81            int x = rand.Next(image.Width);
 82            int y = rand.Next(image.Height);
 83            g.DrawRectangle(new System.Drawing.Pen(System.Drawing.Color.LightGray, 0), x, y, 11);
 84        }

 85
 86        //输出不同字体和颜色的验证码字符
 87
 88        for (int i = 0; i < checkCode.Length; i++)
 89        {
 90            int cindex = rand.Next(3);
 91            int findex = rand.Next(1);
 92
 93            System.Drawing.Font f = new System.Drawing.Font(font[findex], 10, System.Drawing.FontStyle.Bold);
 94            System.Drawing.Brush b = new System.Drawing.SolidBrush(c[cindex]);
 95            int ii = 4;
 96            if ((i + 1% 2 == 0)
 97            {
 98                ii = 2;
 99            }

100            g.DrawString(checkCode.Substring(i, 1), f, b, 3 + (i * 10), ii);
101        }

102        //画一个边框
103
104        g.DrawRectangle(new System.Drawing.Pen(System.Drawing.Color.DarkGray, 0), 00, image.Width - 1, image.Height - 1);
105
106        //输出到浏览器
107        System.IO.MemoryStream ms = new System.IO.MemoryStream();
108        image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
109        context.Response.ClearContent();
110        context.Response.ContentType = "image/Jpeg";
111        context.Response.BinaryWrite(ms.ToArray());
112        g.Dispose();
113        image.Dispose();
114    }

115
116}

117

--------------------------------------------------
页面调用<img src="ashx文件地址">

判断验证码是否正确
Session["Code"].ToString()==输入的字符

转载于:https://www.cnblogs.com/micheng11/archive/2008/03/24/1120056.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值