/// <summary> /// 验证码图片 /// </summary> /// <param name="context"></param> /// <param name="options"></param> public static void Captcha(HttpContextWrapper context, CaptchaModel options) { if (Equals(options, null)) options = CaptchaOptions(); int width = options.Width, height = options.Height; string value = options.Value, cookieName = options.CookieName; var colors = options.Colors; var fonts = options.Fonts; var bmp = new Bitmap(width, height); var g = Graphics.FromImage(bmp); g.Clear(options.BackGroundColor); g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit; for (var i = 0; i < 3; i++) { var x1 = StringConvert.SysRandom.Next(width); var y1 = StringConvert.SysRandom.Next(height); var x2 = StringConvert.SysRandom.Next(width); var y2 = StringConvert.SysRandom.Next(height); var clr = colors[StringConvert.SysRandom.Next(colors.Length)]; g.DrawLine(new Pen(clr), x1, y1, x2, y2); } for (var i = 0; i < value.Length; i++) { var fnt = fonts[StringConvert.SysRandom.Next(fonts.Length)]; var ft = new Font(fnt, 16); var clr = colors[StringConvert.SysRandom.Next(colors.Length)]; g.DrawString(value[i].ToString(), ft, new SolidBrush(clr), (float)i * 20 + 20, 6); } for (var i = 0; i < 50; i++) { var x = StringConvert.SysRandom.Next(bmp.Width); var y = StringConvert.SysRandom.Next(bmp.Height); var clr = colors[StringConvert.SysRandom.Next(colors.Length)]; bmp.SetPixel(x, y, clr); } var ms = new MemoryStream(); try { bmp.Save(ms, ImageFormat.Jpeg); context.Response.ClearContent(); context.Response.ContentType = "image/jpg"; context.Response.BinaryWrite(ms.ToArray()); } finally { bmp.Dispose(); g.Dispose(); ms.Dispose(); } CookieHelper.SetCookie(context, cookieName, BaseCryption.SignData(cookieName, value, HmacType.Sha1)); }
/// <summary> /// 验证码默认选项 /// </summary> /// <returns></returns> private static CaptchaModel CaptchaOptions() { var options = new CaptchaModel { Width = 150, Height = 30, CookieName = "CAPTCHAModel", Colors = new[] { Color.Black, Color.Red, Color.Blue, Color.Green, Color.Orange, Color.Brown, Color.DarkBlue }, BackGroundColor = Color.White, Characters = "23456789ABCDEFGHJKLMNPRSTWXY", Fonts = new[] { "Times New Roman", "MS Mincho", "Book Antiqua", "Gungsuh", "PMingLiU", "Impact" }, Value = string.Empty }; for (var i = 0; i < 6; i++) { options.Value += options.Characters[StringConvert.SysRandom.Next(options.Characters.Length)]; } return options; }
/// <summary> /// 验证验证码是否正确 /// </summary> /// <param name="context"></param> /// <param name="options"></param> /// <param name="value"></param> /// <returns></returns> public static bool VerifyCaptcha(HttpContextWrapper context, CaptchaModel options, string value) { if (Equals(options, null)) options = CaptchaOptions(); return BaseCryption.VerifyData(options.CookieName, CookieHelper.GetCookie(context, options.CookieName), HmacType.Sha1); }