Angular + ABP前后端分离 登录验证码
1.后端
生成验证码
1 /// <summary> 2 /// 自定义长度验证码(字母+数字) 3 /// </summary> 4 /// <param name="length">验证码长度</param> 5 /// <returns></returns> 6 private VerificationCodeDto GenerateCheckCode(int length=4) 7 { 8 int number; 9 string RandomCode = string.Empty; 10 Random r = new Random(); 11 for (int i = 0; i < length; i++) 12 { 13 number = r.Next(); 14 //字符从0~9, A~Z中随机产生,对应的ASCII码分别为48~57, 65~90 a-z 97~122 15 number = number % 36; 16 if (number < 10) 17 number += 48; 18 else 19 number += 55; 20 RandomCode += ((char)number).ToString(); 21 } 22 23 var result = new VerificationCodeDto() { Id = Guid.NewGuid().ToString(), Code = RandomCode }; 24 //在缓存中中保存验证码1分钟有效使用UUID作为唯一标识 25 _cacheManager.GetCache("VerificationCode").Set(result.Id, RandomCode,TimeSpan.FromMinutes(1)); 26 return result; 27 }
生成图片验证码(跨平台写法参考)
1 /// <summary> 2 /// 创建图片验证码 3 /// </summary> 4 public VerificationCodeImageDto CreateCheckCodeImage() 5 { 6 var result = new VerificationCodeImageDto(); 7 var codeInfo = GenerateCheckCode(); 8 //若验证码为空,则直接返回 9 if (codeInfo.Code == null || codeInfo.Code.Trim() == string.Empty) 10 return result; 11 //根据验证码的长度确定输出图片的宽度 12 int iWidth = (int)Math.Ceiling(codeInfo.Code.Length * 15m); 13 int iHeight = 20; 14 //创建图像 15 Bitmap image = new Bitmap(iWidth, iHeight); 16 //从图像获取一个绘图面 17 Graphics g = Graphics.FromImage(image); 18 19 try 20 { 21 Random r = new Random(); 22 //清空图片背景色 23 g.Clear(Color.White); 24 //画图片的背景噪音线10条 25 for (int i = 0; i < 10; i++) 26 { 27 int x1 = r.Next(image.Width); 28 int x2 = r.Next(image.Width); 29 int y1 = r.Next(image.Height); 30 int y2 = r.Next(image.Height); 31 //用银色画出噪音线 32 g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2); 33 } 34 //画图片的前景噪音点50个 35 for (int i = 0; i < 50; i++) 36 { 37 int x = r.Next(image.Width); 38 int y = r.Next(image.Height); 39 image.SetPixel(x, y, Color.FromArgb(r.Next())); 40 } 41 //画图片的框线 42 g.DrawRectangle(new Pen(Color.SaddleBrown), 0, 0, image.Width - 1, image.Height - 1); 43 //定义绘制文字的字体 44 Font f = new Font("Arial", 12, (FontStyle.Bold | FontStyle.Italic)); 45 //线性渐变画刷 46 System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.Purple, 1.2f, true); 47 g.DrawString(codeInfo.Code, f, brush, 2, 2); 48 //创建内存流用于输出图片 49 using (System.IO.MemoryStream ms = new System.IO.MemoryStream()) 50 { 51 image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); 52 byte[] arr = new byte[ms.Length]; 53 ms.Position = 0; 54 ms.Read(arr, 0, (int)ms.Length); 55 ms.Close(); 56 result.Id = codeInfo.Id; 57 result.ImageCode = Convert.ToBase64String(arr); 58 return result; 59 } 60 } 61 finally 62 { 63 //释放Bitmap对象和Graphics对象 64 g.Dispose(); 65 image.Dispose(); 66 } 67 68 }
前端展示
纯验证码
图片验证码
效果:
后端校验
1 /// <summary> 2 /// 校验验证码 3 /// </summary> 4 /// <param name="info"></param> 5 /// <returns></returns> 6 public bool CheckVerificationCode(VerificationCodeDto info) 7 { 8 var code= _cacheManager.GetCache("VerificationCode").GetOrDefault(info.Id); 9 if (code!=null) 10 { 11 if (code.ToString() == info.Code) 12 { 13 return true; 14 } 15 else 16 { 17 return false; 18 } 19 } 20 else 21 { 22 throw new UserFriendlyException("验证码失效"); 23 //return false 24 } 25 }