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。

  • 相关阅读:
    为什么要用Hibernate框架? 把SessionFactory,Session,Transcational封装成包含crud的工具类并且处理了事务,那不是用不着spring了?
    Weblogic12c安装与配置详解
    淘宝自己的前端框架KISSY(类似jquery)
    ExtJS4中initComponent和constructor的区别
    Servlet的getContextPath(), getServletPath(), getRequestURI(), getRealPath("/")
    Hash Join 一定是选择小表作为驱动表吗
    oracle for loop 简单
    oracle正则表达式
    Android开发--用户定位服务--UserLocation
    android蓝牙开发---与蓝牙模块进行通信
  • 原文地址:https://www.cnblogs.com/godbell/p/7143679.html
Copyright © 2011-2022 走看看