asp.net 一般处理程序实现网站验证码

使用VerifyCode.ashx一般处理程序生成验证码,实现如下:

using System;
using System.Drawing;
using System.Web;
using System.Web.SessionState;

namespace Zhong.Web
{
    /// <summary>
    /// VerifyCode 的摘要说明
    /// </summary>
    public class VerifyCode : IHttpHandler,IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "image/gif";
            string code = GetCode(6);   //产生6位随机数
            context.Session["verifycode"] = code;   //保存到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 Font("Arial", 12, (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(code, 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();
            }
        }
        /// <summary>
        /// 产生随机数
        /// </summary>
        /// <param name="length">随机数长度</param>
        /// <returns></returns>
        private string GetCode(int length)
        {
            string str = "0123456789abcdefghijklmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ";
            //char[] letters = str.ToCharArray();
            string code = "";
            Random random = new Random();
            for (int i = 0; i < length; i++)
            {
                code += str.Substring(random.Next(str.Length), 1);
            }
            return code;
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
View Code

 特别注意:一般处理程序需要实现IRequiresSessionState接口,目的是使可以读写Session。

posted @ 2017-07-09 23:37  Jichan·Jong  阅读(201)  评论(0编辑  收藏  举报