生成随机数验证码

/// <summary>
    /// 验证码随机数
    /// </summary>
    public class Captcha
    {
        public static string randomCode = "";//随机验证码字符串
        /// <summary>
        /// 生成随机数
        /// </summary>
        /// <param name="codeLen">数据的长度</param>
        /// <returns></returns>
        public static string CreateRandomCode(int codeLen)
        {
            string allChar = "0,1,2,3,4,5,6,7,8,9";//,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z
            string[] allCharAry = allChar.Split(',');
            randomCode = "";
            int temp = -1;
            Random rand = new Random();
            for (int i = 0; i < codeLen; i++)
            {
                if (temp != -1)
                {
                    rand = new Random(i * temp * ((int)DateTime.Now.Ticks));
                }
                int t = rand.Next(9);
                if (temp == t)
                {
                    return CreateRandomCode(codeLen);
                }
                temp = t;
                randomCode += allCharAry[t];
            }
            return randomCode;
        }
    }

随机数单独放一个类中使用静态字段,静态字段在同一个实例中唯一;

/// <summary>
        /// 根据随机数生成图片
        /// </summary>
        /// <returns></returns>
        private byte[] CreateValidGraphic()
        {
            string validateCode = Captcha.CreateRandomCode(codeLen);
            Bitmap img = new Bitmap((int)Math.Ceiling(validateCode.Length * 16.0), 27);
            Graphics g = Graphics.FromImage(img);
            try
            {
                Random random = new Random();//生成随机数
                g.Clear(Color.White);//清空图片背景色
                for (int i = 0; i < 25; i++)//画图片的干扰线
                {
                    int x1 = random.Next(img.Width);
                    int x2 = random.Next(img.Width);
                    int y1 = random.Next(img.Height);
                    int y2 = random.Next(img.Height);
                    g.DrawLine(new Pen(Color.Silver), x1, x2, y1, y2);
                }
                Font font = new Font("Arial", 13, (FontStyle.Bold | FontStyle.Italic));
                LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, img.Width, img.Height), Color.Blue, Color.DarkRed, 1.2f, true);
                g.DrawString(validateCode, font, brush, 3, 2);
                for (int i = 0; i < 100; i++)//画图片的前景干扰点
                {
                    int x = random.Next(img.Width);
                    int y = random.Next(img.Height);
                    img.SetPixel(x, y, Color.FromArgb(random.Next()));
                }
                g.DrawRectangle(new Pen(Color.Silver), 0, 0, img.Width - 1, img.Height - 1);//画图片的边框线
                MemoryStream stream = new MemoryStream();
                img.Save(stream, ImageFormat.Jpeg);
                return stream.ToArray();//输入图片
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                g.Dispose();
                img.Dispose();
            }
        }

生成图片验证码,根据自己需求调整

/// <summary>
        /// 获取图片验证码
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetCaptcha()
        {
            var bytes = CreateValidGraphic();
            return File(bytes,@"image/Gif");
        }

提供给前端接口

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值