C# 验证码2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 | /// <summary> /// 验证码生成 /// </summary> public class VerificationCodeHelper { //定义颜色 private static Color[] colors = { Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Orange, Color.Brown, Color.DarkCyan, Color.Purple }; //定义字体 private static string font = "宋体" ; //定义验证码字符及出现频次 ,避免出现0 o j i l 1 x; private static String charCollection = "ASDFGHZCVBNMKQWERTYUP" ; //字体大小 private static int fontSize = 23; //旋转角度 private static int angle = 30; /// <summary> /// 验证码生成 /// </summary> /// <param name="guid">缓存标识</param> /// <param name="length">字符个数</param> /// <param name="width">宽度</param> /// <param name="height">高度</param> /// <returns></returns> public static byte [] GetBitmapCode( string guid, int width, int height, int length = 4) { //lk:这里高宽需要写死 或限制最大高宽 避免被攻击者写的太大导致内存占用过高 height = height > 500 ? 500 : height; width = width > 500 ? 500 : width; string codes = "" ; string code; int codeX = width / (length + 1); Random random = new Random(); using (Bitmap image = new Bitmap(width, height)) { using (Graphics g = Graphics.FromImage(image)) { //清空图片背景色 g.Clear(Color.FromArgb(247, 247, 247)); g.FillRectangle( new SolidBrush(Color.FromArgb(247, 247, 247)), 0, 0, image.Width, image.Height); //画图片的背景噪音线 for ( int i = 0; i < 5; i++) { g.DrawLine( new Pen(colors[random.Next(colors.Length)]), random.Next(image.Width), random.Next(image.Height), random.Next(image.Width), random.Next(image.Height)); } //文字距中 StringFormat format = new StringFormat(StringFormatFlags.NoClip); format.Alignment = StringAlignment.Center; format.LineAlignment = StringAlignment.Near; using (Font f = new Font(font, fontSize, FontStyle.Regular)) { using (Brush b = new SolidBrush(colors[random.Next(colors.Length)])) { for ( int i = 0; i < length; i++) { code = charCollection[random.Next(0, charCollection.Length)].ToString(); SizeF sizeF = g.MeasureString(code, f); PointF dot = new PointF(codeX, random.Next(image.Height / 3)); g.TranslateTransform(dot.X, dot.Y); //移动光标到指定位置 int rAngel = random.Next(-angle, angle); g.RotateTransform(rAngel); g.DrawString(code, f, b, 1, 1, format); g.RotateTransform(-rAngel); //转回去 g.TranslateTransform(2, -dot.Y); //移动光标到指定位置 codes += code; } } } //返回图片数据 using (System.IO.MemoryStream ms = new System.IO.MemoryStream()) { image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); SaveToChache(guid, codes); return ms.ToArray(); } } } } /// <summary> /// 保存验证码到内存中 /// </summary> /// <param name="cacheFlag"></param> /// <param name="code"></param> public static void SaveToChache( string cacheFlag, string code) { string cacheName = RedisHelper.GetCacheName(RedisCacheType.VerificationCode, cacheFlag); CacheManager.SetCache(cacheName, code, TimeSpan.FromMinutes(5), ExpirType.Absolute); } /// <summary> /// 验证验证码是否正确 /// </summary> /// <param name="cacheFlag"></param> /// <param name="code"></param> /// <returns></returns> public static string CheckCode( string cacheFlag, string code) { string cacheName = RedisHelper.GetCacheName(RedisCacheType.VerificationCode, cacheFlag); string cacheCode = CacheManager.GetCache(cacheName); if ( string .IsNullOrEmpty(cacheCode)) { return "验证码已过期,请重新输入!" ; } else { CacheManager.RemoveCache(cacheName); if (cacheCode.Equals(code, StringComparison.OrdinalIgnoreCase) == false ) { return "验证码错误,请重新输入!" ; } return null ; } } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)