温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

ASP.NET如何实现验证码以及刷新验证码

发布时间:2021-09-29 18:01:10 来源:亿速云 阅读:329 作者:小新 栏目:开发技术

小编给大家分享一下ASP.NET如何实现验证码以及刷新验证码,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

实现代码

/// <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如何实现验证码以及刷新验证码”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI