asp.net core验证码(非原创,整合网上例子)

 

转载原创)验证码参考网址:

https://blog.csdn.net/ChaITSimpleLove/article/details/80531791

 

首先通过Nuget: Install-Package ZKWeb.System.Drawing 

 1.项目右键新建一个类ValidationCode.cs

添加引用:

using System.DrawingCore;

using System.DrawingCore.Drawing2D;

using System.DrawingCore.Imaging;

using System.IO;

 

 

生产验证码代码;

namespace lwz

{

    public class ValidationCode

    {

        //***********************************************

        public sealed class VerifyCodeHelper

        {

            #region 单例模式

            //创建私有化静态obj锁  

            private static readonly object _ObjLock = new object();

            //创建私有静态字段,接收类的实例化对象  

            private static VerifyCodeHelper _VerifyCodeHelper = null;

            //构造函数私有化  

            private VerifyCodeHelper() { }

            //创建单利对象资源并返回  

            public static VerifyCodeHelper GetSingleObj()

            {

                if (_VerifyCodeHelper == null)

                {

                    lock (_ObjLock)

                    {

                        if (_VerifyCodeHelper == null)

                            _VerifyCodeHelper = new VerifyCodeHelper();

                    }

                }

                return _VerifyCodeHelper;

            }

            #endregion

 

            #region 生产验证码

            public enum VerifyCodeType { NumberVerifyCode, AbcVerifyCode, MixVerifyCode };

 

            /// <summary>

            /// 1.数字验证码

            /// </summary>

            /// <param name="length"></param>

            /// <returns></returns>

            private string CreateNumberVerifyCode(int length)

            {

                int[] randMembers = new int[length];

                int[] validateNums = new int[length];

                string validateNumberStr = "";

                //生成起始序列值  

                int seekSeek = unchecked((int)DateTime.Now.Ticks);

                Random seekRand = new Random(seekSeek);

                int beginSeek = seekRand.Next(0, Int32.MaxValue - length * 10000);

                int[] seeks = new int[length];

                for (int i = 0; i < length; i++)

                {

                    beginSeek += 10000;

                    seeks[i] = beginSeek;

                }

                //生成随机数字  

                for (int i = 0; i < length; i++)

                {

                    Random rand = new Random(seeks[i]);

                    int pownum = 1 * (int)Math.Pow(10, length);

                    randMembers[i] = rand.Next(pownum, Int32.MaxValue);

                }

                //抽取随机数字  

                for (int i = 0; i < length; i++)

                {

                    string numStr = randMembers[i].ToString();

                    int numLength = numStr.Length;

                    Random rand = new Random();

                    int numPosition = rand.Next(0, numLength - 1);

                    validateNums[i] = Int32.Parse(numStr.Substring(numPosition, 1));

                }

                //生成验证码  

                for (int i = 0; i < length; i++)

                {

                    validateNumberStr += validateNums[i].ToString();

                }

 

 

                return validateNumberStr;

            }

 

            /// <summary>

            /// 2.字母验证码

            /// </summary>

            /// <param name="length">字符长度</param>

            /// <returns>验证码字符</returns>

            private string CreateAbcVerifyCode(int length)

            {

                char[] verification = new char[length];

                char[] dictionary = { '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',

                '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'

            };

                Random random = new Random();

                for (int i = 0; i < length; i++)

                {

                    verification[i] = dictionary[random.Next(dictionary.Length - 1)];

                }

                return new string(verification);

            }

 

            /// <summary>

            /// 3.混合验证码

            /// </summary>

            /// <param name="length">字符长度</param>

            /// <returns>验证码字符</returns>

            private string CreateMixVerifyCode(int length)

            {

                char[] verification = new char[length];

                char[] dictionary = { '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',

                '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'

            };

                Random random = new Random();

                for (int i = 0; i < length; i++)

                {

                    verification[i] = dictionary[random.Next(dictionary.Length - 1)];

                }

                return new string(verification);

            }

 

            /// <summary>

            /// 产生验证码(随机产生4-6位)

            /// </summary>

            /// <param name="type">验证码类型:数字,字符,符合</param>

            /// <returns></returns>

            public string CreateVerifyCode(VerifyCodeType type)

            {

                string verifyCode = string.Empty;

                Random random = new Random();

                int length = random.Next(4, 6);

                switch (type)

                {

                    case VerifyCodeType.NumberVerifyCode:

                        verifyCode = GetSingleObj().CreateNumberVerifyCode(length);

                        break;

                    case VerifyCodeType.AbcVerifyCode:

                        verifyCode = GetSingleObj().CreateAbcVerifyCode(length);

                        break;

                    case VerifyCodeType.MixVerifyCode:

                        verifyCode = GetSingleObj().CreateMixVerifyCode(length);

                        break;

                }

                return verifyCode;

            }

            #endregion

 

            #region 验证码图片

            /// <summary>

            /// 验证码图片 => Bitmap

            /// </summary>

            /// <param name="verifyCode">验证码</param>

            /// <param name="width">宽</param>

            /// <param name="height">高</param>

            /// <returns>Bitmap</returns>

            public System.DrawingCore.Bitmap CreateBitmapByImgVerifyCode(string verifyCode, int width, int height)

            {

                System.DrawingCore.Font font = new System.DrawingCore.Font("Arial", 14, (System.DrawingCore.FontStyle.Bold | System.DrawingCore.FontStyle.Italic));

                System.DrawingCore.Brush brush;

                System.DrawingCore.Bitmap bitmap = new System.DrawingCore.Bitmap(width, height);

                System.DrawingCore.Graphics g = System.DrawingCore.Graphics.FromImage(bitmap);

                System.DrawingCore.SizeF totalSizeF = g.MeasureString(verifyCode, font);

                System.DrawingCore.SizeF curCharSizeF;

                System.DrawingCore.PointF startPointF = new System.DrawingCore.PointF(0, (height - totalSizeF.Height) / 2);

                Random random = new Random(); //随机数产生器

                g.Clear(System.DrawingCore.Color.White); //清空图片背景色  

                for (int i = 0; i < verifyCode.Length; i++)

                {

                    brush = new LinearGradientBrush(new System.DrawingCore.Point(0, 0), new System.DrawingCore.Point(1, 1), System.DrawingCore.Color.FromArgb(random.Next(255), random.Next(255), random.Next(255)), System.DrawingCore.Color.FromArgb(random.Next(255), random.Next(255), random.Next(255)));

                    g.DrawString(verifyCode[i].ToString(), font, brush, startPointF);

                    curCharSizeF = g.MeasureString(verifyCode[i].ToString(), font);

                    startPointF.X += curCharSizeF.Width;

                }

 

                //画图片的干扰线  

                for (int i = 0; i < 10; i++)

                {

                    int x1 = random.Next(bitmap.Width);

                    int x2 = random.Next(bitmap.Width);

                    int y1 = random.Next(bitmap.Height);

                    int y2 = random.Next(bitmap.Height);

                    g.DrawLine(new System.DrawingCore.Pen(System.DrawingCore.Color.Silver), x1, y1, x2, y2);

                }

 

                //画图片的前景干扰点  

                for (int i = 0; i < 100; i++)

                {

                    int x = random.Next(bitmap.Width);

                    int y = random.Next(bitmap.Height);

                    bitmap.SetPixel(x, y, System.DrawingCore.Color.FromArgb(random.Next()));

                }

 

                g.DrawRectangle(new System.DrawingCore.Pen(System.DrawingCore.Color.Silver), 0, 0, bitmap.Width - 1, bitmap.Height - 1); //画图片的边框线  

                g.Dispose();

                return bitmap;

            }

 

            /// <summary>

            /// 验证码图片 => byte[]

            /// </summary>

            /// <param name="verifyCode">验证码</param>

            /// <param name="width">宽</param>

            /// <param name="height">高</param>

            /// <returns>byte[]</returns>

            public byte[] CreateByteByImgVerifyCode(string verifyCode, int width, int height)

            {

                System.DrawingCore.Font font = new System.DrawingCore.Font("Arial", 14, (System.DrawingCore.FontStyle.Bold | System.DrawingCore.FontStyle.Italic));

                System.DrawingCore.Brush brush;

                System.DrawingCore.Bitmap bitmap = new System.DrawingCore.Bitmap(width, height);

                System.DrawingCore.Graphics g = System.DrawingCore.Graphics.FromImage(bitmap);

                System.DrawingCore.SizeF totalSizeF = g.MeasureString(verifyCode, font);

                System.DrawingCore.SizeF curCharSizeF;

                System.DrawingCore.PointF startPointF = new System.DrawingCore.PointF(0, (height - totalSizeF.Height) / 2);

                Random random = new Random(); //随机数产生器

                g.Clear(System.DrawingCore.Color.White); //清空图片背景色  

                for (int i = 0; i < verifyCode.Length; i++)

                {

                    brush = new LinearGradientBrush(new System.DrawingCore.Point(0, 0), new System.DrawingCore.Point(1, 1), System.DrawingCore.Color.FromArgb(random.Next(255), random.Next(255), random.Next(255)), System.DrawingCore.Color.FromArgb(random.Next(255), random.Next(255), random.Next(255)));

                    g.DrawString(verifyCode[i].ToString(), font, brush, startPointF);

                    curCharSizeF = g.MeasureString(verifyCode[i].ToString(), font);

                    startPointF.X += curCharSizeF.Width;

                }

 

                //画图片的干扰线  

                for (int i = 0; i < 10; i++)

                {

                    int x1 = random.Next(bitmap.Width);

                    int x2 = random.Next(bitmap.Width);

                    int y1 = random.Next(bitmap.Height);

                    int y2 = random.Next(bitmap.Height);

                    g.DrawLine(new System.DrawingCore.Pen(System.DrawingCore.Color.Silver), x1, y1, x2, y2);

                }

 

                //画图片的前景干扰点  

                for (int i = 0; i < 100; i++)

                {

                    int x = random.Next(bitmap.Width);

                    int y = random.Next(bitmap.Height);

                    bitmap.SetPixel(x, y, System.DrawingCore.Color.FromArgb(random.Next()));

                }

 

                g.DrawRectangle(new System.DrawingCore.Pen(System.DrawingCore.Color.Silver), 0, 0, bitmap.Width - 1, bitmap.Height - 1); //画图片的边框线  

                g.Dispose();

 

                //保存图片数据  

                MemoryStream stream = new MemoryStream();

                bitmap.Save(stream, ImageFormat.Jpeg);

                //输出图片流  

                return stream.ToArray();

 

            }

            #endregion

 

        }

    }

}

 

 2.新建一个验证码控制器:ValidationCodeController

添加引用:

using static lwz.ValidationCode;

using System.DrawingCore.Imaging;

using System.IO;

using Microsoft.AspNetCore.Http;

 

 

代码:

namespace lwz.Controllers

{

    public class ValidationCodeController : Controller

{

 

 

/// <summary>

/// 数字验证码

/// </summary>

/// <returns></returns>

public FileContentResult NumberVerifyCode()

{

    string code = VerifyCodeHelper.GetSingleObj().CreateVerifyCode(VerifyCodeHelper.VerifyCodeType.NumberVerifyCode);

byte[] codeImage = VerifyCodeHelper.GetSingleObj().CreateByteByImgVerifyCode(code, 100, 40);

 HttpContext.Session.SetString("Code", code);

    return File(codeImage, @"image/jpeg");

}

 

/// <summary>

/// 字母验证码

/// </summary>

/// <returns></returns>

public FileContentResult AbcVerifyCode()

{

    string code = VerifyCodeHelper.GetSingleObj().CreateVerifyCode(VerifyCodeHelper.VerifyCodeType.AbcVerifyCode);

    var bitmap = VerifyCodeHelper.GetSingleObj().CreateBitmapByImgVerifyCode(code, 100, 40);

    MemoryStream stream = new MemoryStream();

bitmap.Save(stream, ImageFormat.Png);

 HttpContext.Session.SetString("Code", code);

    return File(stream.ToArray(), "image/png");

}

 

        public IActionResult ValidationCode()

        {

            /// <summary>

            /// 混合验证码

            /// </summary>

            /// <returns></returns>

 

            string code = VerifyCodeHelper.GetSingleObj().CreateVerifyCode(VerifyCodeHelper.VerifyCodeType.MixVerifyCode);

            var bitmap = VerifyCodeHelper.GetSingleObj().CreateBitmapByImgVerifyCode(code, 100, 40);

            MemoryStream stream = new MemoryStream();

            bitmap.Save(stream, ImageFormat.Gif);

            HttpContext.Session.SetString("Code", code);

 

            return File(stream.ToArray(), "image/gif");

        }

    }

}

 

 3.在其他需要引用“验证码控制器中的方法”的控制器编写代码

  [HttpGet]

        public IActionResult TeacherLogin(int i)

        {

            return View();

        }

        [HttpPost]

        public IActionResult TeacherLogin(string Teachername, string Password, string Class, string Code_input)

        {

            string Code = HttpContext.Session.GetString("Code");

            if (string.IsNullOrEmpty(Code_input))

            {

                var script = String.Format("<script>alert('验证码不为空,请重新登录!');location.href='{0}'</script>", Url.Action("TeacherLogin", "Teacher"));

                return Content(script, "text/html");

            }

            if (Code != Code_input)

            {

                var script = String.Format("<script>alert('验证码错误,请重新登录!');location.href='{0}'</script>", Url.Action("TeacherLogin", "Teacher"));

                return Content(script, "text/html");

            }

}

 

4.登陆视图

 <div class="Code_place">

                        @*<img id="nubImg" title="数字验证码" src="/Admin/AdminCode" alt="vcode" onclick="refresh()" style="cursor:pointer;" />*@

                        <img id="mixImg" title="数字字母混合验证码" src="/ValidationCode/ValidationCode" alt="vcode" onclick="this.src=this.src+'?'" style="cursor:pointer;" />

                    <p  style="color:blue;font-size:14px;">看不清?点击图片</p>

                    </div>

                    <script type="text/javascript">

                        function refresh() {

                            var id = document.getElementById("nubImg");

                            var str = "/ValidationCode/ValidationCode?random=" + Math.random();

                            id.setAttribute("src", str);

                        }

                    </script>

posted @ 2018-09-13 11:18  风葬秋暝  阅读(789)  评论(0编辑  收藏  举报