来源:https://blog.csdn.net/Yuhang_Zhou/article/details/140614304
验证码辅助类
验证码辅助类
using System.Drawing; using System.Drawing.Imaging; namespace XCGApp { /// <summary> /// 验证码辅助类 /// </summary> public class ValidateCodeUtil { /// <summary> /// 验证码长度 /// </summary> public int CodeLen { get; set; } /// <summary> /// 验证码 /// </summary> public string VCode { get; private set; } /// <summary> /// 验证码图像 /// </summary> public Bitmap VCodeImg { get; private set; } private int fineness = 70;//图片清晰度 数值越大越清晰 private int imgWidth = 48;//图片宽度 private int imgHeight = 24;//图片高度 private int fontSize = 14;//字体大小 private string fontFamily = "Microsoft YaHei";//字体名称 /// <summary> /// 生成验证码 /// </summary> public string CreateVCode(int codeLen = 4) { CodeLen = codeLen; string vCode = ""; Random random = new Random(); for (int i = 0; i < CodeLen; i++) { int n = random.Next(10); vCode += n.ToString(); } VCode = vCode; return vCode; } /// <summary> /// 验证码图像Byte /// </summary> /// <returns></returns> public byte[] GetCodeImgByte() { VCodeImg = new Bitmap(imgWidth, imgHeight); //生成像素点 DrawPixel(VCodeImg); //绘制验证码图像 DrewVCode(VCodeImg, VCode); //Bitmap转byte[] byte[] imgByte = BitmapToByte(VCodeImg); return imgByte; } /// <summary> /// 验证码图像Byte /// </summary> /// <returns></returns> public byte[] GetCodeImgByte(string code) { VCodeImg = new Bitmap(imgWidth, imgHeight); //生成像素点 DrawPixel(VCodeImg); //绘制验证码图像 DrewVCode(VCodeImg, code); //Bitmap转byte[] byte[] imgByte = BitmapToByte(VCodeImg); return imgByte; } /// <summary> /// 生成像素点 /// </summary> private void DrawPixel(Bitmap bitmap) { Random random = new Random(); for (int i = 0; i < bitmap.Width; i++) { for (int j = 0; j < bitmap.Height; j++) { if (random.Next(90) <= fineness) bitmap.SetPixel(i, j, Color.LightGray); } } } /// <summary> /// 绘制验证码图像 /// </summary> /// <param name="bitmap"></param> private void DrewVCode(Bitmap bitmap, string vCode) { Graphics g = Graphics.FromImage(bitmap); Font font = new Font(fontFamily, fontSize, FontStyle.Regular); g.DrawString(vCode, font, Brushes.Black, 0, 0); } /// <summary> /// Bitmap转byte[] /// </summary> private byte[] BitmapToByte(Bitmap bitmap) { MemoryStream ms = new MemoryStream(); bitmap.Save(ms, ImageFormat.Bmp); byte[] imgByte = ms.GetBuffer(); return imgByte; } } }
api接口
[HttpGet] public ActionResult ShowCode(string key) { var codeObj = RedisUtil.GetRedis().Get(key); if (codeObj == null) { ApiResult res = new ApiResult() { Code = "-1", Message = "验证码失效,请刷新重试。" }; return Json(res); } else { string vCode = codeObj.ToString(); //生成验证码图像Byte ValidateCodeUtil validateCode = new ValidateCodeUtil(); byte[] imgByte = validateCode.GetCodeImgByte(vCode); return File(imgByte, "image/jpeg"); } }
api接口生成验证码
[HttpPost] public ApiResult GenerateValidateNo([FromBody]VCDto dto) { ApiResult res = new ApiResult() { Code = "-1", Message = "请求失败" }; try { if (!string.IsNullOrEmpty(dto.OldKey)) { //移除旧验证码 RedisUtil.GetRedis().Remove(dto.OldKey); } //验证码的RedisKey var guid = Guid.NewGuid().ToString().Replace("-", ""); //生成验证码 ValidateCodeUtil validateCode = new ValidateCodeUtil(); string vCode = validateCode.CreateVCode(); //保存进Redis,1分钟有效。 RedisUtil.GetRedis().Insert(guid, vCode, DateTime.Now.AddMinutes(1)); res.Code = ""; res.Message = "OK"; res.Data = guid; return res; } catch (Exception e) { res.Message = "验证码生成异常:" + e.Message; return res; } }
vue前端<img id="imgvcode" ref="imgvcode" src="" @click="GetVerifyCode()" />
mounted() { this.GetVerifyCode(); }, methods: { GetVerifyCode() { let vcSrc = that.$refs.imgvcode.src; var q = vcSrc.match(new RegExp('[?&]' + 'key' + '=([^&#]*)')); let oldKey = (q && q[1]) || ''; let para = { "OldKey": oldKey }; PostRequestAPI("Test/GenerateValidateNo", para).then((res) => { if (res.Code == "") { that.form.validateCodeKey = res.Data; that.$refs.imgvcode.src = '/Test/ShowCode?key=' + res.Data; } else { alert(res.Message); } }); }, }