欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

ASP.NET 实现验证码以及刷新验证码的小例子

程序员文章站 2024-03-31 14:42:52
实现代码 复制代码 代码如下:///     /// 生成验证码图片,保存session名称verificat...

实现代码

复制代码 代码如下:

/// <summary>
    /// 生成验证码图片,保存session名称verificationcode
    /// </summary>
    public static void createverificationcode()
    {
        int number;
        string checkcode = string.empty;

        //随机数种子
        random randoms = new random();

        for (int i = 0; i < 4; i++) //校验码长度为4
        {
            //随机的整数
            number = randoms.next();

            //字符从0-9,a-z中随机产生,对应的ascii码分别为
            //48-57,65-90
            number = number % 36;
            if (number < 10)
            {
                number += 48;
            }
            else
            {
                number += 55;
            }
            checkcode += ((char)number).tostring();
        }

        //在session中保存校验码
        system.web.httpcontext.current.session["verificationcode"] = checkcode;

        //若校验码为空,则直接返回
        if (checkcode == null || checkcode.trim() == string.empty)
        {
            return;
        }
        //根据校验码的长度确定输出图片的长度
        system.drawing.bitmap image = new system.drawing.bitmap(55, 20);//(int)math.ceiling(convert.todouble(checkcode.length * 15))
        //创建graphics对象
        graphics g = graphics.fromimage(image);
        try
        {
            //生成随机数种子
            random random = new random();
            //清空图片背景色
            g.clear(color.white);
            //画图片的背景噪音线 10条
            //---------------------------------------------------
            for (int i = 0; i < 10; i++)
            {
                //噪音线起点坐标(x1,y1),终点坐标(x2,y2)
                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);
            }
            //---------------------------------------------------
            //brush b = brushes.silver;
            //g.fillrectangle(b, 0, 0, image.width, image.height);
            //---------------------以上两种任选其一------------------------------
            //输出图片中校验码的字体: 12号arial,粗斜体
            font font = new font("arial", 12, (fontstyle.bold | fontstyle.italic));

            //线性渐变画刷
            lineargradientbrush brush = new lineargradientbrush(new rectangle(0, 0, image.width, image.height), color.blue, color.purple, 1.2f, true);
            g.drawstring(checkcode, font, brush, 2, 2);

            //画图片的前景噪音点 50个
            for (int i = 0; i < 50; i++)
            {
                int x = random.next(image.width);
                int y = random.next(image.height);
                image.setpixel(x, y, color.fromargb(random.next()));
            }

            //画图片的边框线
            g.drawrectangle(new pen(color.peru), 0, 0, image.width - 1, image.height - 1);

            //创建内存流用于输出图片
            using (memorystream ms = new memorystream())
            {
                //图片格式指定为png
                image.save(ms, imageformat.jpeg);
                //清除缓冲区流中的所有输出
                system.web.httpcontext.current.response.clearcontent();
                //输出流的http mime类型设置为"image/png"
                system.web.httpcontext.current.response.contenttype = "image/jpeg";
                //输出图片的二进制流
                system.web.httpcontext.current.response.binarywrite(ms.toarray());
            }
        }
        finally
        {
            //释放bitmap对象和graphics对象
            g.dispose();
            image.dispose();
        }
    }

创建一个aspx页面

复制代码 代码如下:

 <%@ page language="c#" autoeventwireup="true" codefile="authcode.aspx.cs" inherits="authcode" %>

 <%help.createverificationcode(); %>

添加html代码,引用

复制代码 代码如下:

 <div class="positionr">
     <label>验证码:</label>
     <span class="style1"> *</span>
     <input type="text" class="yanzm" runat="server" reg="^.+$" id="txtauthcode" tip="请输入验证码!" />
     <img class="yanzm_img" src="authcode.aspx" alt="" id="imgauthcode" />
 </div>

如何实现刷新?

复制代码 代码如下:

     <script type="text/javascript">
         $("#imgauthcode").click(function () {
             $(this).attr("src", "authcode.aspx?code=" + (new date()).gettime());
         });
     </script>

效果图

ASP.NET 实现验证码以及刷新验证码的小例子

实例下载