1、验证码帮助类
namespace IdeaCore.Services.Common { public class ValidateCodeService : IValidateCodeService { private const string ValidateCodeKey = "ValidateCode"; private const double Pi2 = 6.283185307179586476925286766559; private const string BackColor = "#dde3ec"; /// <summary> /// 获取验证码的sessionkey /// </summary> /// <returns></returns> public string GetValidateCodeKey() { return ValidateCodeKey; } /// <summary> /// 生成验证码 /// </summary> /// <param name="length"></param> /// <returns></returns> public string CreateValidateCode(int length) { string code = string.Empty; while (true) { var s = (Guid.NewGuid() + "").Substring(0, 1); if (s == "1" || s == "l" || s == "o" || s == "0") { } else { code += s; } if (code.Length >= length) { break; } } return code; } /// <summary> /// 创建验证码的图片 /// </summary> /// <param name="validateCode">验证码</param> public byte[] CreateValidateGraphic(string validateCode) { const int fSize = 40; const int fWidth = fSize + 8; int imageWidth = validateCode.Length * fWidth + 4 + 8 * 2; const int imageHeight = fSize * 2 + 8; Color[] colors = { Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Orange, Color.Brown, Color.DarkCyan, Color.Purple }; string[] fonts = { "Arial", "Georgia" }; var image = new Bitmap(imageWidth, imageHeight); Graphics g = Graphics.FromImage(image); try { g.Clear(ColorTranslator.FromHtml(BackColor)); var rand = new Random(); ////给背景添加随机生成的燥点 //Pen pen = new Pen(Color.RoyalBlue, 0); // int c = 6 * 20; //杂点数量 // for (int i = 0; i < c; i++) // { // int x = rand.Next(image.Width); // int y = rand.Next(image.Height); // g.DrawRectangle(pen, x, y, 1, 1); // } const int n1 = (imageHeight - 40 - 8 * 2); const int n2 = n1 / 4; const int top1 = n2; const int top2 = n2 * 2; //随机字体和颜色的验证码字符 for (int i = 0; i < validateCode.Length; i++) { int cindex = rand.Next(colors.Length - 1); int findex = rand.Next(fonts.Length - 1); var f = new Font(fonts[findex], fSize, FontStyle.Bold); Brush b = new SolidBrush(colors[cindex]); var top = i % 2 == 1 ? top2 : top1; var left = i * fWidth; if (i == 0) { left += 8; } g.DrawString(validateCode.Substring(i, 1), f, b, left, top); } // image = TwistImage(image, true, new Random().Next(3, 10), new Random().Next(0, 6)); var stream = new MemoryStream(); image.Save(stream, ImageFormat.Jpeg); //输出图片流 return stream.ToArray(); } finally { g.Dispose(); image.Dispose(); } } #region 产生波形滤镜效果 /// <summary> /// 正弦曲线Wave扭曲图片(Edit By 51aspx.com) /// </summary> /// <param name="srcBmp">图片路径</param> /// <param name="bXDir">如果扭曲则选择为True</param> /// <param name="dMultValue"></param> /// <param name="dPhase">波形的起始相位,取值区间[0-2*PI)</param> /// <returns></returns> private static Bitmap TwistImage(Bitmap srcBmp, bool bXDir, double dMultValue, double dPhase) { var destBmp = new Bitmap(srcBmp.Width, srcBmp.Height); // 将位图背景填充为白色 var graph = Graphics.FromImage(destBmp); graph.FillRectangle(new SolidBrush(ColorTranslator.FromHtml(BackColor)), 0, 0, destBmp.Width, destBmp.Height); graph.Dispose(); double dBaseAxisLen = bXDir ? destBmp.Height : destBmp.Width; for (int i = 0; i < destBmp.Width; i++) { for (int j = 0; j < destBmp.Height; j++) { var dx = bXDir ? (Pi2 * j) / dBaseAxisLen : (Pi2 * i) / dBaseAxisLen; dx += dPhase; var dy = Math.Cos(dx); // 取得当前点的颜色 int nOldX = bXDir ? i + (int)(dy * dMultValue) : i; int nOldY = bXDir ? j : j + (int)(dy * dMultValue); var color = srcBmp.GetPixel(i, j); if (nOldX >= 0 && nOldX < destBmp.Width && nOldY >= 0 && nOldY < destBmp.Height) { destBmp.SetPixel(nOldX, nOldY, color); } } } return destBmp; } #endregion } }
2、获取验证码
/// <summary> /// 获取验证码 /// </summary> /// <returns></returns> public FileContentResult GetValidateCode() { var code = _validateCodeService.CreateValidateCode(4); var bytes = _validateCodeService.CreateValidateGraphic(code); HttpContext.Session.SetString(_validateCodeService.GetValidateCodeKey(), code); return File(bytes, @"image/jpeg"); }
3、验证验证码
/// <summary> /// 验证验证码 /// </summary> /// <param name="code"></param> /// <returns></returns> protected bool ValidateCode(string code) { if (string.IsNullOrEmpty(code)) { return false; } var sessionCode = HttpContext.Session.GetString("ValidateCode"); return sessionCode != null && sessionCode == code.Trim(); }
分类: .Net Core