using Microsoft.Extensions.Caching.Memory; using System.DrawingCore; using System.DrawingCore.Imaging; using System.Text; using YBEE.EQM.Core; namespace YBEE.EQM.Application { /// /// 常规验证码 /// public class GeneralCaptchaService : IGeneralCaptchaService, ITransient { private readonly IMemoryCache _memoryCache; public GeneralCaptchaService(IMemoryCache memoryCache) { _memoryCache = memoryCache; } /// /// 生成验证码图片 /// /// 验证码长度 /// 图片宽度 /// 图片高度 /// 字体大小 /// public GeneralCaptchaOutput CreateCaptchaImage(int length = 6, int width = 260, int height = 36, int fontSize = 22) { var rtnResult = new GeneralCaptchaOutput(); var code = GenerateRandom(length); // 随机字符串集合 rtnResult.ImageBase64 = Convert.ToBase64String(Draw(code, width, height, fontSize)); rtnResult.Token = Guid.NewGuid().ToString(); // 缓存验证码正确位置集合 var cacheOptions = new MemoryCacheEntryOptions().SetSlidingExpiration(TimeSpan.FromSeconds(120)); _memoryCache.Set(CommonConst.CACHE_KEY_CODE + rtnResult.Token, code, cacheOptions); return rtnResult; } /// /// 验证码验证 /// /// /// public GeneralCaptchaOutput CheckCode(GeneralCaptchaInput input) { var res = new GeneralCaptchaOutput(); var code = _memoryCache.Get(CommonConst.CACHE_KEY_CODE + input.Token); if (code == null) { res.Code = 1; res.Message = "验证码已失效,请重新获取"; return res; } if (string.Compare(input.CaptchaCode, (string)code, true) != 0) { res.Code = 2; res.Message = "验证码错误"; return res; } _memoryCache.Remove(CommonConst.CACHE_KEY_CODE + input.Token); res.Code = 0; res.Message = "验证成功"; return res; } #region 私有方法 /// /// 绘制验证码 /// /// /// /// /// /// private static byte[] Draw(string code, int codeW, int codeH, int fontSize) { // 颜色列表,用于验证码、噪线、噪点 Color[] color = { Color.Black, Color.Red, Color.Blue, Color.Green, Color.Orange, Color.Brown, Color.Brown, Color.DarkBlue }; // 字体列表,用于验证码 string[] fonts = new[] { "Times New Roman", "Verdana", "Arial", "Gungsuh", "Impact" }; using (var bmp = new Bitmap(codeW, codeH)) using (var g = Graphics.FromImage(bmp)) using (var ms = new MemoryStream()) { g.Clear(Color.White); var rnd = new Random(); // 画噪线 for (int i = 0; i < 4; i++) { int x1 = rnd.Next(codeW); int y1 = rnd.Next(codeH); int x2 = rnd.Next(codeW); int y2 = rnd.Next(codeH); var clr = color[rnd.Next(color.Length)]; g.DrawLine(new Pen(clr), x1, y1, x2, y2); } // 画验证码字符串 string fnt; Font ft; for (int i = 0; i < code.Length; i++) { fnt = fonts[rnd.Next(fonts.Length)]; ft = new Font(fnt, fontSize); var clr = color[rnd.Next(color.Length)]; g.DrawString(code[i].ToString(), ft, new SolidBrush(clr), (float)i * 24 + 3, (float)0); } // 将验证码图片写入内存流 bmp.Save(ms, ImageFormat.Png); return ms.ToArray(); } } /// /// 生成验证码 /// /// /// private static string GenerateRandom(int length) { var chars = new StringBuilder(); // 验证码的字符集,去掉了一些容易混淆的字符 char[] character = { '2', '3', '4', '5', '6', '8', '9', 'a', 'b', 'd', 'e', 'f', 'h', 'k', 'm', 'n', 'r', 'x', 'y', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'R', 'S', 'T', 'W', 'X', 'Y' }; Random rnd = new(); // 生成验证码字符串 for (int i = 0; i < length; i++) { chars.Append(character[rnd.Next(character.Length)]); } return chars.ToString(); } #endregion } }