.Net 后台登陆验证码的实现

在这里插入图片描述

.Net 后台登陆实现不重复的验证码
作者:秋名
撰写时间:2020 年 9 月 17 日

html.

  @*验证码*@
                            <div class="form-group">
                                <label class="col-sm-2 col-md-offset-1 control-label no-padding-right" for="validCode">
                                    验证码
                                </label>
                                <div class="col-sm-8 ">
                                    <div class="input-group">
                                        <input type="text" name="validCode" id="validCode" class="form-control" />
                                        <div class="input-group-addon" style="padding:0"><img src="/Main/ValideCode" id="ValidateCode" /></div>
                                    </div>
                                </div>
                            </div>

script.

  <script>
        //点击图片刷新验证码
        $("#ValidateCode").click(function () {
            //浏览器有一个缓存机制,这里借用时间是为了生成不重复图片。
            $("#ValidateCode").attr("src", "/Main/ValideCode?="+new Date());
        });
    </script>

控制器:

 #region 方法
        public ActionResult ValideCode()//生成验证码
        {
            //调用封装好的ValidCodeUtils里面的方法生成4位数随机字符串(扣号数字多少生成多少)
            string strValideCode = Common.ValidCodeUtils.GetRandomCode(4);
            //调用封装好的方法把我们生成的随机字符串转化为一张图片
            byte[] btValideCode = Common.ValidCodeUtils.CreateImage(strValideCode);
            //把我们生成的图片以文件形式File返回(文件内容,文件类型)
            return File(btValideCode, @"image/jpeg");
        }
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;


namespace 2020.Common
{
    public static class ValidCodeUtils
    { /// <summary>
      /// 获得随机字符串
      /// </summary>
      /// <param name="intLength">随机数的长度</param>
      /// <returns>随机数字符串</returns>
        public static string GetRandomCode(int intLength)
        {
            /*产生数字和密码混合的随机数*/
            string strReturn = string.Empty;
            Random random = new Random();//随机数
            for (int i = 0; i < intLength; i++)
            {
                char cRerult;
                int intRandom = random.Next();//产生一个非负随机整数
                /*根据当前随机数来确定字符串*/
                //intRandom % 3 获取的是intRandom/3 得到的余数
                if (intRandom % 3 == 0)
                {
                    //产生数字
                    //位数来产生数字
                    cRerult = (char)(0x30 + (intRandom % 10));
                }
                else if (intRandom % 3 == 1)
                {
                    //位数产生大写字母:大写字符 65-97 A 65
                    //68 D  25 Z
                    cRerult = (char)(0x41 + (intRandom % 0x1a));
                }
                else
                {
                    //余数为2
                    //产生小写字母 98-116
                    cRerult = (char)(0x61 + (intRandom % 0x1a));
                }
                strReturn += cRerult.ToString();
            }
            return strReturn;
        }

        /// <summary>
        /// 根据字符串创建验证码
        /// </summary>
        /// <param name="strRandom">字符串</param>
        /// <returns>图片</returns>
        public static byte[] CreateImage(string strRandom)
        {
            //新增图片
            Bitmap newBitmap = new Bitmap(strRandom.Length * 20, 30);

            Graphics g = Graphics.FromImage(newBitmap);
            g.Clear(Color.White);
            //在图片上绘制文字
            SolidBrush solidBrush = new SolidBrush(Color.Red);
            g.DrawString(strRandom, new Font("Aril", 17), solidBrush, 12, 1);
            //在图片上绘制干扰线
            Random random = new Random();
            for (int i = 0; i < 10; i++)
            {
                //产生一条线,并绘制到画布。 起始点(x,y)  总结点
                int x1 = random.Next(newBitmap.Width);
                int y1 = random.Next(newBitmap.Height);
                int x2 = random.Next(newBitmap.Width);
                int y2 = random.Next(newBitmap.Height);
                g.DrawLine(new Pen(Color.DarkGray), x1, y1, x2, y2);
            }
            //绘制图片的前景干扰点
            for (int i = 0; i < 100; i++)
            {
                int x = random.Next(newBitmap.Width);
                int y = random.Next(newBitmap.Height);
                newBitmap.SetPixel(x, y, Color.FromArgb(random.Next()));
            }
            //在最外边绘制边框
            g.DrawRectangle(new Pen(Color.Blue), 0, 0, newBitmap.Width, newBitmap.Height);
            g.DrawRectangle(new Pen(Color.Blue), -1, -1, newBitmap.Width, newBitmap.Height);
            //将图转保存到内存流中
            MemoryStream ms = new MemoryStream();
            newBitmap.Save(ms, ImageFormat.Jpeg);
            return ms.ToArray();//将流内容写入byte数组返回
        }
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值