zoukankan      html  css  js  c++  java
  • 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。

  • 相关阅读:
    Linux网络检测手段汇总
    Hadoop单机搭建
    Nginx之Location模块
    关于STM32驱动DS1302实时时钟的一点思考
    灵感手环第一步——0.96寸OLED显示实验
    把简单做到极致
    KEIL MDK 查看代码量、RAM使用情况--RO-data、RW-data、ZI-data
    关于SPI通信原理与程序实现
    【转载】keil5中加入STM32F10X_HD,USE_STDPERIPH_DRIVER的原因
    关于keil5使用注意事项(预定义、路径包含)
  • 原文地址:https://www.cnblogs.com/godbell/p/7143679.html
Copyright © 2011-2022 走看看