init commit
This commit is contained in:
227
20220330_Vote/Ewide.Core/Captcha/ClickWord/ClickWordCaptcha.cs
Normal file
227
20220330_Vote/Ewide.Core/Captcha/ClickWord/ClickWordCaptcha.cs
Normal file
@@ -0,0 +1,227 @@
|
||||
using Furion;
|
||||
using Furion.DependencyInjection;
|
||||
using Furion.DistributedIDGenerator;
|
||||
using Furion.JsonSerialization;
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Drawing;
|
||||
using System.Drawing.Imaging;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
|
||||
namespace Ewide.Core
|
||||
{
|
||||
/// <summary>
|
||||
/// 点选验证码
|
||||
/// </summary>
|
||||
public class ClickWordCaptcha : IClickWordCaptcha, ITransient
|
||||
{
|
||||
private readonly IMemoryCache _memoryCache;
|
||||
|
||||
public ClickWordCaptcha(IMemoryCache memoryCache)
|
||||
{
|
||||
_memoryCache = memoryCache;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 生成验证码图片
|
||||
/// </summary>
|
||||
/// <param name="code"></param>
|
||||
/// <param name="width"></param>
|
||||
/// <param name="height"></param>
|
||||
/// <returns></returns>
|
||||
public ClickWordCaptchaResult CreateCaptchaImage(string code, int width, int height)
|
||||
{
|
||||
var rtnResult = new ClickWordCaptchaResult();
|
||||
|
||||
// 变化点: 3个字
|
||||
int rightCodeLength = 3;
|
||||
|
||||
Bitmap bitmap = null;
|
||||
Graphics g = null;
|
||||
MemoryStream ms = null;
|
||||
Random random = new Random();
|
||||
|
||||
Color[] colorArray = { Color.Black, Color.DarkBlue, Color.Green, Color.Orange, Color.Brown, Color.DarkCyan, Color.Purple };
|
||||
|
||||
string bgImagesDir = Path.Combine(App.WebHostEnvironment.WebRootPath, "Captcha/Image");
|
||||
string[] bgImagesFiles = Directory.GetFiles(bgImagesDir);
|
||||
//if (bgImagesFiles == null || bgImagesFiles.Length == 0)
|
||||
// throw Oops.Oh("背景图片文件丢失");
|
||||
|
||||
// 字体来自:https://www.zcool.com.cn/special/zcoolfonts/
|
||||
string fontsDir = Path.Combine(App.WebHostEnvironment.WebRootPath, "Captcha/Font");
|
||||
string[] fontFiles = new DirectoryInfo(fontsDir)?.GetFiles()
|
||||
?.Where(m => m.Extension.ToLower() == ".ttf")
|
||||
?.Select(m => m.FullName).ToArray();
|
||||
//if (fontFiles == null || fontFiles.Length == 0)
|
||||
// throw Oops.Oh("字体文件丢失");
|
||||
|
||||
int imgIndex = random.Next(bgImagesFiles.Length);
|
||||
string randomImgFile = bgImagesFiles[imgIndex];
|
||||
var imageStream = Image.FromFile(randomImgFile);
|
||||
|
||||
bitmap = new Bitmap(imageStream, width, height);
|
||||
imageStream.Dispose();
|
||||
g = Graphics.FromImage(bitmap);
|
||||
Color[] penColor = { Color.Red, Color.Green, Color.Blue };
|
||||
int code_length = code.Length;
|
||||
List<string> words = new List<string>();
|
||||
for (int i = 0; i < code_length; i++)
|
||||
{
|
||||
int colorIndex = random.Next(colorArray.Length);
|
||||
int fontIndex = random.Next(fontFiles.Length);
|
||||
Font f = LoadFont(fontFiles[fontIndex], 15, FontStyle.Regular);
|
||||
Brush b = new SolidBrush(colorArray[colorIndex]);
|
||||
int _y = random.Next(height);
|
||||
if (_y > (height - 30))
|
||||
_y = _y - 60;
|
||||
|
||||
int _x = width / (i + 1);
|
||||
if ((width - _x) < 50)
|
||||
{
|
||||
_x = width - 60;
|
||||
}
|
||||
string word = code.Substring(i, 1);
|
||||
if (rtnResult.repData.point.Count < rightCodeLength)
|
||||
{
|
||||
// (int, int) percentPos = ToPercentPos((width, height), (_x, _y));
|
||||
// 添加正确答案 位置数据
|
||||
rtnResult.repData.point.Add(new PointPosModel()
|
||||
{
|
||||
X = _x, //percentPos.Item1,
|
||||
Y = _y //percentPos.Item2,
|
||||
});
|
||||
words.Add(word);
|
||||
}
|
||||
g.DrawString(word, f, b, _x, _y);
|
||||
}
|
||||
rtnResult.repData.wordList = words;
|
||||
|
||||
ms = new MemoryStream();
|
||||
bitmap.Save(ms, ImageFormat.Jpeg);
|
||||
g.Dispose();
|
||||
bitmap.Dispose();
|
||||
ms.Dispose();
|
||||
rtnResult.repData.originalImageBase64 = Convert.ToBase64String(ms.GetBuffer()); //"data:image/jpg;base64," +
|
||||
rtnResult.repData.token = IDGen.NextID().ToString();
|
||||
|
||||
// 缓存验证码正确位置集合
|
||||
var cacheOptions = new MemoryCacheEntryOptions().SetSlidingExpiration(TimeSpan.FromSeconds(30));
|
||||
_memoryCache.Set(CommonConst.CACHE_KEY_CODE + rtnResult.repData.token, rtnResult.repData.point, cacheOptions);
|
||||
|
||||
rtnResult.repData.point = null; // 清空位置信息
|
||||
return rtnResult;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 转换为相对于图片的百分比单位
|
||||
/// </summary>
|
||||
/// <param name="widthAndHeight">图片宽高</param>
|
||||
/// <param name="xAndy">相对于图片的绝对尺寸</param>
|
||||
/// <returns>(int:xPercent, int:yPercent)</returns>
|
||||
private (int, int) ToPercentPos((int, int) widthAndHeight, (int, int) xAndy)
|
||||
{
|
||||
(int, int) rtnResult = (0, 0);
|
||||
// 注意: int / int = int (小数部分会被截断)
|
||||
rtnResult.Item1 = (int)(((double)xAndy.Item1) / ((double)widthAndHeight.Item1) * 100);
|
||||
rtnResult.Item2 = (int)(((double)xAndy.Item2) / ((double)widthAndHeight.Item2) * 100);
|
||||
|
||||
return rtnResult;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 加载字体
|
||||
/// </summary>
|
||||
/// <param name="path">字体文件路径,包含字体文件名和后缀名</param>
|
||||
/// <param name="size">大小</param>
|
||||
/// <param name="fontStyle">字形(常规/粗体/斜体/粗斜体)</param>
|
||||
private Font LoadFont(string path, int size, FontStyle fontStyle)
|
||||
{
|
||||
var pfc = new System.Drawing.Text.PrivateFontCollection();
|
||||
pfc.AddFontFile(path);// 字体文件路径
|
||||
Font myFont = new Font(pfc.Families[0], size, fontStyle);
|
||||
return myFont;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 随机绘制字符串
|
||||
/// </summary>
|
||||
/// <param name="number"></param>
|
||||
/// <returns></returns>
|
||||
public string RandomCode(int number)
|
||||
{
|
||||
var str = "左怀军天地玄黄宇宙洪荒日月盈昃辰宿列张寒来暑往秋收冬藏闰余成岁律吕调阳云腾致雨露结为霜金生丽水玉出昆冈剑号巨阙珠称夜光果珍李柰菜重芥姜海咸河淡鳞潜羽翔龙师火帝鸟官人皇始制文字乃服衣裳推位让国有虞陶唐吊民伐罪周发殷汤坐朝问道垂拱平章爱育黎首臣伏戎羌遐迩体率宾归王";
|
||||
char[] str_char_arrary = str.ToArray();
|
||||
Random rand = new Random();
|
||||
HashSet<string> hs = new HashSet<string>();
|
||||
bool randomBool = true;
|
||||
while (randomBool)
|
||||
{
|
||||
if (hs.Count == number)
|
||||
break;
|
||||
int rand_number = rand.Next(str_char_arrary.Length);
|
||||
hs.Add(str_char_arrary[rand_number].ToString());
|
||||
}
|
||||
return string.Join("", hs);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 验证码验证
|
||||
/// </summary>
|
||||
/// <param name="input"></param>
|
||||
/// <returns></returns>
|
||||
public dynamic CheckCode(ClickWordCaptchaInput input)
|
||||
{
|
||||
var res = new ClickWordCaptchaResult();
|
||||
|
||||
var rightVCodePos = _memoryCache.Get(CommonConst.CACHE_KEY_CODE + input.Token) as List<PointPosModel>;
|
||||
if (rightVCodePos == null)
|
||||
{
|
||||
res.repCode = "6110";
|
||||
res.repMsg = "验证码已失效,请重新获取";
|
||||
return res;
|
||||
}
|
||||
|
||||
var userVCodePos = JSON.GetJsonSerializer().Deserialize<List<PointPosModel>>(input.PointJson);
|
||||
if (userVCodePos == null || userVCodePos.Count < rightVCodePos.Count)
|
||||
{
|
||||
res.repCode = "6111";
|
||||
res.repMsg = "验证码无效";
|
||||
return res;
|
||||
}
|
||||
|
||||
int allowOffset = 20; // 允许的偏移量(点触容错)
|
||||
for (int i = 0; i < userVCodePos.Count; i++)
|
||||
{
|
||||
var xOffset = userVCodePos[i].X - rightVCodePos[i].X;
|
||||
var yOffset = userVCodePos[i].Y - rightVCodePos[i].Y;
|
||||
xOffset = Math.Abs(xOffset); // x轴偏移量
|
||||
yOffset = Math.Abs(yOffset); // y轴偏移量
|
||||
// 只要有一个点的任意一个轴偏移量大于allowOffset,则验证不通过
|
||||
if (xOffset > allowOffset || yOffset > allowOffset)
|
||||
{
|
||||
res.repCode = "6112";
|
||||
res.repMsg = "验证码错误";
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
_memoryCache.Remove(CommonConst.CACHE_KEY_CODE + input.Token);
|
||||
res.repCode = "0000";
|
||||
res.repMsg = "验证成功";
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 记录正确位置
|
||||
/// </summary>
|
||||
public class PointPosModel
|
||||
{
|
||||
public int X { get; set; }
|
||||
|
||||
public int Y { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
using Furion.DependencyInjection;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace Ewide.Core
|
||||
{
|
||||
/// <summary>
|
||||
/// 点击验证码输入参数
|
||||
/// </summary>
|
||||
[SuppressSniffer]
|
||||
public class ClickWordCaptchaInput
|
||||
{
|
||||
/// <summary>
|
||||
/// 验证码类型
|
||||
/// </summary>
|
||||
[Required(ErrorMessage = "验证码类型")]
|
||||
public string CaptchaType { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 坐标点集合
|
||||
/// </summary>
|
||||
[Required(ErrorMessage = "坐标点集合不能为空")]
|
||||
public string PointJson { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Token
|
||||
/// </summary>
|
||||
public string Token { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
using Furion.DependencyInjection;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Ewide.Core
|
||||
{
|
||||
/// <summary>
|
||||
/// 验证码输出参数
|
||||
/// </summary>
|
||||
[SuppressSniffer]
|
||||
public class ClickWordCaptchaResult
|
||||
{
|
||||
public string repCode { get; set; } = "0000";
|
||||
public string repMsg { get; set; }
|
||||
public RepData repData { get; set; } = new RepData();
|
||||
public bool error { get; set; }
|
||||
public bool success { get; set; } = true;
|
||||
}
|
||||
|
||||
[SuppressSniffer]
|
||||
public class RepData
|
||||
{
|
||||
public string captchaId { get; set; }
|
||||
public string projectCode { get; set; }
|
||||
public string captchaType { get; set; }
|
||||
public string captchaOriginalPath { get; set; }
|
||||
public string captchaFontType { get; set; }
|
||||
public string captchaFontSize { get; set; }
|
||||
public string secretKey { get; set; }
|
||||
public string originalImageBase64 { get; set; }
|
||||
public List<PointPosModel> point { get; set; } = new List<PointPosModel>();
|
||||
public string jigsawImageBase64 { get; set; }
|
||||
public List<string> wordList { get; set; } = new List<string>();
|
||||
public string pointList { get; set; }
|
||||
public string pointJson { get; set; }
|
||||
public string token { get; set; }
|
||||
public bool result { get; set; }
|
||||
public string captchaVerification { get; set; }
|
||||
}
|
||||
|
||||
[SuppressSniffer]
|
||||
public class WordList
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
namespace Ewide.Core
|
||||
{
|
||||
public interface IClickWordCaptcha
|
||||
{
|
||||
dynamic CheckCode(ClickWordCaptchaInput input);
|
||||
ClickWordCaptchaResult CreateCaptchaImage(string code, int width, int height);
|
||||
string RandomCode(int number);
|
||||
}
|
||||
}
|
||||
130
20220330_Vote/Ewide.Core/Captcha/General/GeneralCaptcha.cs
Normal file
130
20220330_Vote/Ewide.Core/Captcha/General/GeneralCaptcha.cs
Normal file
@@ -0,0 +1,130 @@
|
||||
using Furion.DependencyInjection;
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
using System;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Drawing;
|
||||
using System.Drawing.Imaging;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
|
||||
namespace Ewide.Core
|
||||
{
|
||||
/// <summary>
|
||||
/// 常规验证码
|
||||
/// </summary>
|
||||
public class GeneralCaptcha : IGeneralCaptcha, ITransient
|
||||
{
|
||||
private readonly IMemoryCache _memoryCache;
|
||||
|
||||
public GeneralCaptcha(IMemoryCache memoryCache)
|
||||
{
|
||||
_memoryCache = memoryCache;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 生成验证码图片
|
||||
/// </summary>
|
||||
/// <param name="length"></param>
|
||||
/// <returns></returns>
|
||||
public string CreateCaptchaImage(int length = 4)
|
||||
{
|
||||
return Convert.ToBase64String(Draw(length));
|
||||
}
|
||||
|
||||
private string GenerateRandom(int length)
|
||||
{
|
||||
var chars = new StringBuilder();
|
||||
// 验证码的字符集,去掉了一些容易混淆的字符
|
||||
char[] character = { '2', '3', '4', '5', '6', '8', '9', 'a', 'b', 'd', 'e', 'f', 'h', 'k', 'm', 'n', 'r', 'x', 'y', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'R', 'S', 'T', 'W', 'X', 'Y' };
|
||||
Random rnd = new Random();
|
||||
// 生成验证码字符串
|
||||
for (int i = 0; i < length; i++)
|
||||
{
|
||||
chars.Append(character[rnd.Next(character.Length)]);
|
||||
}
|
||||
return chars.ToString();
|
||||
}
|
||||
|
||||
private byte[] Draw(int length = 4)
|
||||
{
|
||||
int codeW = 110;
|
||||
int codeH = 36;
|
||||
int fontSize = 22;
|
||||
|
||||
// 颜色列表,用于验证码、噪线、噪点
|
||||
Color[] color = { Color.Black, Color.Red, Color.Blue, Color.Green, Color.Orange, Color.Brown, Color.Brown, Color.DarkBlue };
|
||||
// 字体列表,用于验证码
|
||||
string[] fonts = new[] { "Times New Roman", "Verdana", "Arial", "Gungsuh", "Impact" };
|
||||
|
||||
var code = GenerateRandom(length); // 随机字符串集合
|
||||
|
||||
using (Bitmap bmp = new Bitmap(codeW, codeH))
|
||||
using (Graphics g = Graphics.FromImage(bmp))
|
||||
using (MemoryStream ms = new MemoryStream())
|
||||
{
|
||||
g.Clear(Color.White);
|
||||
Random rnd = new Random();
|
||||
// 画噪线
|
||||
for (int i = 0; i < 1; i++)
|
||||
{
|
||||
int x1 = rnd.Next(codeW);
|
||||
int y1 = rnd.Next(codeH);
|
||||
int x2 = rnd.Next(codeW);
|
||||
int y2 = rnd.Next(codeH);
|
||||
var clr = color[rnd.Next(color.Length)];
|
||||
g.DrawLine(new Pen(clr), x1, y1, x2, y2);
|
||||
}
|
||||
|
||||
// 画验证码字符串
|
||||
string fnt;
|
||||
Font ft;
|
||||
for (int i = 0; i < code.Length; i++)
|
||||
{
|
||||
fnt = fonts[rnd.Next(fonts.Length)];
|
||||
ft = new Font(fnt, fontSize);
|
||||
var clr = color[rnd.Next(color.Length)];
|
||||
g.DrawString(code[i].ToString(), ft, new SolidBrush(clr), (float)i * 24 + 2, (float)0);
|
||||
}
|
||||
|
||||
// 缓存验证码正确集合
|
||||
var cacheOptions = new MemoryCacheEntryOptions().SetSlidingExpiration(TimeSpan.FromSeconds(30));
|
||||
_memoryCache.Set(CommonConst.CACHE_KEY_CODE + Guid.NewGuid().ToString(), code, cacheOptions);
|
||||
|
||||
// 将验证码图片写入内存流
|
||||
bmp.Save(ms, ImageFormat.Png);
|
||||
return ms.ToArray();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 验证码验证
|
||||
/// </summary>
|
||||
/// <param name="input"></param>
|
||||
/// <returns></returns>
|
||||
public dynamic CheckCode(GeneralCaptchaInput input)
|
||||
{
|
||||
var res = new ClickWordCaptchaResult();
|
||||
|
||||
var code = _memoryCache.Get(CommonConst.CACHE_KEY_CODE + input.Token);
|
||||
if (code == null)
|
||||
{
|
||||
res.repCode = "6110";
|
||||
res.repMsg = "验证码已失效,请重新获取";
|
||||
return res;
|
||||
}
|
||||
if (input.CaptchaCode != (string)code)
|
||||
{
|
||||
res.repCode = "6112";
|
||||
res.repMsg = "验证码错误";
|
||||
return res;
|
||||
}
|
||||
|
||||
_memoryCache.Remove(CommonConst.CACHE_KEY_CODE + input.Token);
|
||||
res.repCode = "0000";
|
||||
res.repMsg = "验证成功";
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
using Furion.DependencyInjection;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace Ewide.Core
|
||||
{
|
||||
/// <summary>
|
||||
/// 常规验证码输入
|
||||
/// </summary>
|
||||
[SuppressSniffer]
|
||||
public class GeneralCaptchaInput
|
||||
{
|
||||
/// <summary>
|
||||
/// 验证码类型
|
||||
/// </summary>
|
||||
[Required(ErrorMessage = "验证码类型")]
|
||||
public string CaptchaType { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 验证码字符
|
||||
/// </summary>
|
||||
[Required(ErrorMessage = "验证码字符不能为空")]
|
||||
public string CaptchaCode { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Token
|
||||
/// </summary>
|
||||
public string Token { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
namespace Ewide.Core
|
||||
{
|
||||
public interface IGeneralCaptcha
|
||||
{
|
||||
dynamic CheckCode(GeneralCaptchaInput input);
|
||||
string CreateCaptchaImage(int length = 4);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user