验证码的种类与实现 C#封装类 - .NET MVC WEBFORM

验证码方式

 

1、随机字母或者数字,纯文本验证码

这种非常容易破解 ,市场上有大量的现成接口或者工具,背景越复杂难度越高。

 

2、题库验证码

要破解这种验证码,需要人工收集题库才可以破解,可以免疫不是专门针对你一个网站的黑客,所以我偏爱这种验证码

1+1=?

•本论坛的域名是?

•今天是星期几?

•复杂点的数学运算



但是他到底是不是就不能破解了呢?

缺点:数量有限。破解方式也很简单,多刷新几次,建立题库和对应的答案,用正则从网页里抓取问题,寻找匹配的答案后破解。也有些用随机生成的数学公式,比如 随机数 [+-*/]随机运算符 随机数=?,小学生水平的程序员也可以搞定……


题库越多高全系数越高


 3、干扰型题库验证码

  将文字显示进行处理起到干扰效果,就算你有正确题库也不能轻易破解

 

 

今天正好身体不舒服请了天假整理了这么一个可以实现上面三种验证码的类

 
效果图

 

实现步骤

 

1、MVC Action 或者 WebForm的 pageLoad写以下代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
public void ValidateImg()
       {
           VerifyCodeSugar v = new VerifyCodeSugar();
 
           //是否随机字体颜色
           v.SetIsRandomColor = true;
           //随机码的旋转角度
           v.SetRandomAngle = 4;
           //文字大小
           v.SetFontSize = 15;
           //背景色
           //v.SetBackgroundColor
           //前景噪点数量
           //v.SetForeNoisePointCount = 3;
           //v.SetFontColor =Color.Red;
           //...还有更多设置不介绍了
 
           var questionList = new Dictionary<string, string>()
           {
 
               {"1+1=?","2"},
               {"喜羊羊主角叫什么名字?","喜羊羊" },
               {"【我爱你】中间的那个字?","爱" },
           };
 
           var questionItem = v.GetQuestion();//不赋值为随机验证码 例如: 1*2=? 这种
 
           //指定验证文本
           //v.SetVerifyCodeText
 
           v.SetVerifyCodeText = questionItem.Key;
 
           Session["VerifyCode"] = questionItem.Value;
 
           //输出图片
           v.OutputImage(System.Web.HttpContext.Current.Response);
 
       }

  

 

2、前台设用action或者pageload页面地址,js给src更换url添加随机参数 达到刷新验证码的功能

 <img src="/File/ValidateImg" />

 

3、完整封装类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
 
namespace Idea.Models
{
 
    /// <summary>
    /// 验证码类
    /// </summary>
    public class VerifyCodeSugar
    {
        private Random objRandom = new Random();
 
        #region setting
 
        /// <summary>
        /// //验证码长度
        /// </summary>
        public int SetLength = 4;
        /// <summary>
        /// 验证码字符串
        /// </summary>
        public string SetVerifyCodeText { get; set; }
        /// <summary>
        /// 是否加入小写字母
        /// </summary>
        public bool SetAddLowerLetter = false;
        /// <summary>
        /// 是否加入大写字母
        /// </summary>
        public bool SetAddUpperLetter = false;
        /// <summary>
        /// 字体大小
        /// </summary>
        public int SetFontSize = 18;
        /// <summary>
        ///  //字体颜色
        /// </summary>
        public Color SetFontColor = Color.Blue;
        /// <summary>
        /// 字体类型
        /// </summary>
        public string SetFontFamily = "Verdana";
        /// <summary>
        /// 背景色
        /// </summary>
        public Color SetBackgroundColor = Color.AliceBlue;
        /// <summary>
        /// 前景噪点数量
        /// </summary>
        public int SetForeNoisePointCount = 2;
        /// <summary>
        /// 随机码的旋转角度
        /// </summary>
        public int SetRandomAngle = 40;
 
        /// <summary>
        /// 是否随机字体颜色
        /// </summary>
        public bool SetIsRandomColor = false;
        /// <summary>
        /// 图片宽度
        /// </summary>
        private int SetWith
        {
            get
            {
                return this.SetVerifyCodeText.Length * SetFontSize;
            }
        }
        /// <summary>
        /// 图片高度
        /// </summary>
        private int SetHeight
        {
            get
            {
                return Convert.ToInt32((60.0 / 100) * SetFontSize + SetFontSize);
            }
        }
        #endregion
 
        #region Constructor Method
        public VerifyCodeSugar()
        {
            this.GetVerifyCodeText();
        }
        #endregion
 
        #region Private Method
        /// <summary>
        /// 得到验证码字符串
        /// </summary>
        private void GetVerifyCodeText()
        {
 
            //没有外部输入验证码时随机生成
            if (String.IsNullOrEmpty(this.SetVerifyCodeText))
            {
                StringBuilder objStringBuilder = new StringBuilder();
 
                //加入数字1-9
                for (int i = 1; i <= 9; i++)
                {
                    objStringBuilder.Append(i.ToString());
                }
 
                //加入大写字母A-Z,不包括O
                if (this.SetAddUpperLetter)
                {
                    char temp = ' ';
 
                    for (int i = 0; i < 26; i++)
                    {
                        temp = Convert.ToChar(i + 65);
 
                        //如果生成的字母不是'O'
                        if (!temp.Equals('O'))
                        {
                            objStringBuilder.Append(temp);
                        }
                    }
                }
 
                //加入小写字母a-z,不包括o
                if (this.SetAddLowerLetter)
                {
                    char temp = ' ';
 
                    for (int i = 0; i < 26; i++)
                    {
                        temp = Convert.ToChar(i + 97);
 
                        //如果生成的字母不是'o'
                        if (!temp.Equals('o'))
                        {
                            objStringBuilder.Append(temp);
                        }
                    }
                }
 
                //生成验证码字符串
                {
                    int index = 0;
 
                    for (int i = 0; i < SetLength; i++)
                    {
                        index = objRandom.Next(0, objStringBuilder.Length);
 
                        this.SetVerifyCodeText += objStringBuilder[index];
 
                        objStringBuilder.Remove(index, 1);
                    }
                }
            }
        }
 
        /// <summary>
        /// 得到验证码图片
        /// </summary>
        private Bitmap GetVerifyCodeImage()
        {
            Bitmap result = null;
 
            //创建绘图
            result = new Bitmap(SetWith, SetHeight);
 
            using (Graphics objGraphics = Graphics.FromImage(result))
            {
                objGraphics.SmoothingMode = SmoothingMode.HighQuality;
 
                //清除整个绘图面并以指定背景色填充
                objGraphics.Clear(this.SetBackgroundColor);
 
                //创建画笔
                using (SolidBrush objSolidBrush = new SolidBrush(this.SetFontColor))
                {
                    this.AddForeNoisePoint(result);
 
                    this.AddBackgroundNoisePoint(result, objGraphics);
 
                    //文字居中
                    StringFormat objStringFormat = new StringFormat(StringFormatFlags.NoClip);
 
                    objStringFormat.Alignment = StringAlignment.Center;
                    objStringFormat.LineAlignment = StringAlignment.Center;
 
                    //字体样式
                    Font objFont = new Font(this.SetFontFamily, objRandom.Next(this.SetFontSize - 3, this.SetFontSize), FontStyle.Regular);
 
                    //验证码旋转,防止机器识别
                    char[] chars = this.SetVerifyCodeText.ToCharArray();
 
                    for (int i = 0; i < chars.Length; i++)
                    {
                        //转动的度数
                        float angle = objRandom.Next(-this.SetRandomAngle, this.SetRandomAngle);
 
                        objGraphics.TranslateTransform(12, 12);
                        objGraphics.RotateTransform(angle);
                        objGraphics.DrawString(chars[i].ToString(), objFont, objSolidBrush, -2, 2, objStringFormat);
                        objGraphics.RotateTransform(-angle);
                        objGraphics.TranslateTransform(2, -12);
                    }
                }
            }
 
            return result;
        }
 
        /// <summary>
        /// 添加前景噪点
        /// </summary>
        /// <param name="objBitmap"></param>
        private void AddForeNoisePoint(Bitmap objBitmap)
        {
            for (int i = 0; i < objBitmap.Width * this.SetForeNoisePointCount; i++)
            {
                objBitmap.SetPixel(objRandom.Next(objBitmap.Width), objRandom.Next(objBitmap.Height), this.SetFontColor);
            }
        }
 
        /// <summary>
        /// 添加背景噪点
        /// </summary>
        /// <param name="objBitmap"></param>
        /// <param name="objGraphics"></param>
        private void AddBackgroundNoisePoint(Bitmap objBitmap, Graphics objGraphics)
        {
            using (Pen objPen = new Pen(Color.Azure, 0))
            {
                for (int i = 0; i < objBitmap.Width * 2; i++)
                {
                    objGraphics.DrawRectangle(objPen, objRandom.Next(objBitmap.Width), objRandom.Next(objBitmap.Height), 1, 1);
                }
            }
 
        }
 
        /// <summary>
        /// 获取随机颜色
        /// </summary>
        /// <returns></returns>
        private Color GetRandomColor()
        {
            Random RandomNum_First = new Random((int)DateTime.Now.Ticks);
            // 对于C#的随机数,没什么好说的
            System.Threading.Thread.Sleep(RandomNum_First.Next(50));
            Random RandomNum_Sencond = new Random((int)DateTime.Now.Ticks);
            // 为了在白色背景上显示,尽量生成深色
            int int_Red = RandomNum_First.Next(256);
            int int_Green = RandomNum_Sencond.Next(256);
            int int_Blue = (int_Red + int_Green > 400) ? 0 : 400 - int_Red - int_Green;
            int_Blue = (int_Blue > 255) ? 255 : int_Blue;
            return Color.FromArgb(int_Red, int_Green, int_Blue);
        }
        #endregion
 
        #region Public Method
        /// <summary>
        /// 输出验证码图片
        /// </summary>
        /// <param name="objHttpResponse">Http响应实例</param>
        /// <returns>输出是否成功</returns>
        public bool OutputImage(HttpResponse objHttpResponse)
        {
            bool result = false;
            if (this.SetIsRandomColor)
            {
                this.SetFontColor = GetRandomColor(); ;
            }
 
            using (Bitmap objBitmap = this.GetVerifyCodeImage())
            {
                if (objBitmap != null)
                {
                    using (MemoryStream objMS = new MemoryStream())
                    {
                        objBitmap.Save(objMS, ImageFormat.Jpeg);
 
                        HttpContext.Current.Response.ClearContent();
                        HttpContext.Current.Response.ContentType = "image/Jpeg";
                        HttpContext.Current.Response.BinaryWrite(objMS.ToArray());
                        HttpContext.Current.Response.Flush();
                        HttpContext.Current.Response.End();
 
                        result = true;
                    }
                }
            }
 
            return result;
        }
 
        /// <summary>
        /// 获取问题
        /// </summary>
        /// <param name="questionList">默认数字加减验证</param>
        /// <returns></returns>
        public KeyValuePair<string, string> GetQuestion(Dictionary<string, string> questionList = null)
        {
            if (questionList == null)
            {
                questionList = new Dictionary<string, string>();
                var operArray = new string[] { "+", "*", "num" };
                var left = objRandom.Next(0, 10);
                var right = objRandom.Next(0, 10);
                var oper = operArray[objRandom.Next(0, operArray.Length)];
                if (oper == "+")
                {
                    string key = string.Format("{0}+{1}=?", left, right);
                    string val = (left + right).ToString();
                    questionList.Add(key, val);
                }
                else if (oper == "*")
                {
                    string key = string.Format("{0}×{1}=?", left, right);
                    string val = (left * right).ToString();
                    questionList.Add(key, val);
                }
                else
                {
                    var num = objRandom.Next(1000, 9999); ;
                    questionList.Add(num.ToString(), num.ToString());
                }
            }
            return questionList.ToList()[objRandom.Next(0, questionList.Count)];
        }
        #endregion
    }
 
}

  

为嘛就一个类写这么多,没排版博客园不给推荐到首页。。

 

posted @   阿妮亚  阅读(2765)  评论(3编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
点击右上角即可分享
微信分享提示