登录  /  注册

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

高洛峰
发布: 2017-01-13 14:27:52
原创
1090人浏览过

实现代码

/// <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实现验证码以及刷新验证码的小例子

更多ASP.NET实现验证码以及刷新验证码的小例子相关文章请关注PHP中文网!

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
关于CSS思维导图的课件在哪? 课件
凡人来自于2024-04-16 10:10:18
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号