.net生成验证码

时间:2022-06-01 19:37:12

本文为大家分享了.net生成验证码所有代码,大家可以动手操作一下,会有意想不到的收获。

先给大家看看效果图:

.net生成验证码

页面代码:

?
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
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
  <title>验证码</title>
  <script type="text/javascript">
    //换一张
    function change() {
      var img = document.getElementById("ValidateCode");
      img.src = img.src + '?';
    }
  </script>
</head>
<body>
  <form id="form1" runat="server">
  <div>
    <table>
      <tr>
        <th>
          验证码
        </th>
        <td>
          <asp:TextBox runat="server" ID="tbCheckCode" CssClass="Small" MaxLength="4"></asp:TextBox>
        </td>
        <td>
          <img src="VerifyCode.aspx" id="ValidateCode" alt="换一张" onclick="javascript:change()" />
        </td>
        <td>
          <a id="ChangeCode" href="#" onclick="javascript:change()">换一张</a>
        </td>
      </tr>
      <tr>
        <td>
          <asp:Button runat="server" ID="LoginSubmit" OnClick="LoginSubmits" Text="登录" />
        </td>
      </tr>
    </table>
  </div>
  </form>
</body>
</html>

后台代码:

?
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
    /// <summary>
    /// 登录
    /// </summary>
    /// <param name="sender"></param>
    protected void LoginSubmits(object sender, EventArgs e)
    {
      CheckLogin();
    }
 
    #region 私有方法
    #region
    public static Boolean IsNotNull(Object str)
    {
      return !IsNull(str);
    }
    public static Boolean IsNull(Object str)
    {
      return str == null;
    }
    public static void Show(System.Web.UI.Page page, string msg)
    {
      page.ClientScript.RegisterStartupScript(page.GetType(), "message",
        "<script language='javascript' defer>alert('" + msg.ToString() + "');</script>");
    }
    #endregion
 
    private void CheckLogin()
    {
      if (tbCheckCode.Text.Trim() == "")
      {
        Show(this, "验证码不能为空!");
        return;
      }
      else
      {
        if (IsNotNull(Session["ValidateCheckCode"]))
        {
          if (tbCheckCode.Text.Trim() != Session["ValidateCheckCode"].ToString())
          {
            Show(this, "验证码填写不正确!");
            tbCheckCode.Text = "";
            return;
          }
        }
        else
        {
          Show(this, "请刷新验证码!");
          return;
        }
      }
    }
    #endregion
  }
}

生成验证码:

?
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
340
using System;
using System.Drawing;
using System.Text;
using System.Web;
 
namespace Web.TEXT
{
  public partial class VerifyCode : System.Web.UI.Page
  {
    public Encoding GB = Encoding.GetEncoding("GB2312");
 
    protected void Page_Load(object sender, EventArgs e)
    {
      VerifyCode v = new VerifyCode();
 
      v.Length = this.length;
      v.FontSize = this.fontSize;
      v.Chaos = this.chaos;
      v.BackgroundColor = this.backgroundColor;
      v.ChaosColor = this.chaosColor;
      v.CodeSerial = this.codeSerial;
      v.Colors = this.colors;
      v.Fonts = this.fonts;
      v.Padding = this.padding;
      string code = v.CreateVerifyCode(); //取随机码
      v.CreateImageOnPage(code, this.Context); // 输出图片
      Response.Cookies.Add(new HttpCookie("CheckCode", code.ToUpper())); // 使用Cookies取验证码的值
 
      System.Web.HttpContext.Current.Session["ValidateCheckCode"] = code;
      //return checkCode;
    }
 
    #region 验证码长度(默认4个验证码的长度)
 
    private int length = 4;
 
    public int Length
    {
      get { return length; }
      set { length = value; }
    }
 
    #endregion
 
    #region 验证码字体大小(为了显示扭曲效果,默认40像素,可以自行修改)
 
    private int fontSize = 12;
 
    public int FontSize
    {
      get { return fontSize; }
      set { fontSize = value; }
    }
 
    #endregion
 
    #region 边框补(默认1像素)
 
    private int padding = 1;
 
    public int Padding
    {
      get { return padding; }
      set { padding = value; }
    }
 
    #endregion
 
    #region 是否输出燥点(默认不输出)
 
    private bool chaos = true;
 
    public bool Chaos
    {
      get { return chaos; }
      set { chaos = value; }
    }
 
    #endregion
 
    #region 输出燥点的颜色(默认灰色)
 
    private Color chaosColor = Color.LightGray;
 
    public Color ChaosColor
    {
      get { return chaosColor; }
      set { chaosColor = value; }
    }
 
    #endregion
 
    #region 自定义背景色(默认白色)
 
    private Color backgroundColor = Color.White;
 
    public Color BackgroundColor
    {
      get { return backgroundColor; }
      set { backgroundColor = value; }
    }
 
    #endregion
 
    #region 自定义随机颜色数组
 
    private Color[] colors =
    {
      Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Orange, Color.Brown,
      Color.DarkCyan, Color.Purple
    };
 
    public Color[] Colors
    {
      get { return colors; }
      set { colors = value; }
    }
 
    #endregion
 
    #region 自定义字体数组
 
    private string[] fonts = {"Arial", "Georgia"};
 
    public string[] Fonts
    {
      get { return fonts; }
      set { fonts = value; }
    }
 
    #endregion
 
    #region 自定义随机码字符串序列(使用逗号分隔)
 
    private string codeSerial = "0,1,2,3,4,5,6,7,8,9";
    // string codeSerial = "0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z";
    // string codeSerial = "殃,央,鸯,秧,杨,扬,佯,疡,羊,洋,阳,氧,仰,痒,养,样,漾,邀,腰,妖,瑶,摇,尧,遥,窑,谣,姚,咬,舀,药,要,耀,椰,噎,耶,爷,野,冶,也,页,掖,业,叶,曳,腋,夜,液,一,壹,医,揖,铱,依,伊,衣,颐,夷,遗,移,仪,胰,疑,沂,宜,姨,彝,椅,蚁,倚,已,乙,矣,以,艺,抑,易,邑,屹,亿,役,臆,逸,肄,疫,亦,裔,意,毅,忆,义,益,溢,诣,议,谊,译,异,翼,翌,绎,茵,荫,因,殷,音,阴,姻,吟,银,淫,寅,饮,尹,引,隐,印,英,樱,婴,鹰,应,缨,莹,萤,营,荧,蝇,迎,赢,盈,影,颖,硬,映,哟,拥,佣,臃,痈,庸,雍,踊,蛹,咏,泳,涌,永,恿,勇,用,幽,优,悠,忧,尤,由,邮,铀,犹,油,游,酉,有,友,右,佑,釉,诱,又,幼,迂,淤,于,盂,榆,虞,愚,舆,余,俞,逾,鱼,愉,渝,渔,隅,予,娱,雨,与,屿,禹,株,蛛,朱,猪,诸,诛,逐,竹,烛,煮,拄,瞩,嘱,主,著,柱,助,蛀,贮,铸,筑,住,注,祝,驻,抓,爪,拽,专,砖,转,撰,赚,篆,桩,庄,装,妆,撞,壮,状,椎,锥,追,赘,坠,缀,谆,准,捉,拙,卓,桌,琢,茁,酌,啄,着,灼,浊,兹,咨,资,姿,滋,淄,孜,紫,仔,籽,滓,子,自,渍,字,鬃,棕,踪,宗,综,总,纵,邹,走,奏,揍,租,足,卒,族,祖,诅,阻,组,钻,纂,嘴,醉,最,罪,尊,遵,昨,左,佐,柞,做,作,坐,座";
    public string CodeSerial
    {
      get { return codeSerial; }
      set { codeSerial = value; }
    }
 
    #endregion
 
    //产生波形滤镜效果
 
    #region 产生波形滤镜效果
 
    private const double PI = 3.1415926535897932384626433832795;
    private const double PI2 = 6.283185307179586476925286766559;
 
    /**/
 
    /// <summary>
    /// 正弦曲线Wave扭曲图片(Edit By 51aspx.com)
    /// </summary>
    /// <param name="srcBmp">图片路径</param>
    /// <param name="bXDir">如果扭曲则选择为True</param>
    /// <param name="nMultValue">波形的幅度倍数,越大扭曲的程度越高,一般为3</param>
    /// <param name="dPhase">波形的起始相位,取值区间[0-2*PI)</param>
    /// <returns></returns>
    public System.Drawing.Bitmap TwistImage(Bitmap srcBmp, bool bXDir, double dMultValue, double dPhase)
    {
      System.Drawing.Bitmap destBmp = new Bitmap(srcBmp.Width, srcBmp.Height);
 
      // 将位图背景填充为白色
      System.Drawing.Graphics graph = System.Drawing.Graphics.FromImage(destBmp);
      graph.FillRectangle(new SolidBrush(System.Drawing.Color.White), 0, 0, destBmp.Width, destBmp.Height);
      graph.Dispose();
 
      double dBaseAxisLen = bXDir ? (double) destBmp.Height : (double) destBmp.Width;
 
      for (int i = 0; i < destBmp.Width; i++)
      {
        for (int j = 0; j < destBmp.Height; j++)
        {
          double dx = 0;
          dx = bXDir ? (PI2*(double) j)/dBaseAxisLen : (PI2*(double) i)/dBaseAxisLen;
          dx += dPhase;
          double dy = Math.Sin(dx);
 
          // 取得当前点的颜色
          int nOldX = 0, nOldY = 0;
          nOldX = bXDir ? i + (int) (dy*dMultValue) : i;
          nOldY = bXDir ? j : j + (int) (dy*dMultValue);
 
          System.Drawing.Color color = srcBmp.GetPixel(i, j);
          if (nOldX >= 0 && nOldX < destBmp.Width
            && nOldY >= 0 && nOldY < destBmp.Height)
          {
            destBmp.SetPixel(nOldX, nOldY, color);
          }
        }
      }
 
      return destBmp;
    }
 
 
 
    #endregion
 
    #region 生成校验码图片
 
    public Bitmap CreateImageCode(string code)
    {
      int fSize = FontSize;
      int fWidth = fSize + Padding;
 
      int imageWidth = (int) (code.Length*fWidth) + 4 + Padding*2;
      int imageHeight = fSize*2 + Padding;
 
      System.Drawing.Bitmap image = new System.Drawing.Bitmap(imageWidth, imageHeight);
 
      Graphics g = Graphics.FromImage(image);
 
      g.Clear(BackgroundColor);
 
      Random rand = new Random();
 
      //给背景添加随机生成的燥点
      if (this.Chaos)
      {
 
        Pen pen = new Pen(ChaosColor, 0);
        int c = Length*10;
 
        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);
        }
      }
 
      int left = 0, top = 0, top1 = 1, top2 = 1;
 
      int n1 = (imageHeight - FontSize - Padding*2);
      int n2 = n1/4;
      top1 = n2;
      top2 = n2*2;
 
      Font f;
      Brush b;
 
      int cindex, findex;
 
      #region 随机字体和颜色的验证码字符
 
      for (int i = 0; i < code.Length; i++)
      {
        cindex = rand.Next(Colors.Length - 1);
        findex = rand.Next(Fonts.Length - 1);
 
        f = new System.Drawing.Font(Fonts[findex], fSize, System.Drawing.FontStyle.Bold);
        b = new System.Drawing.SolidBrush(Colors[cindex]);
 
        if (i%2 == 1)
        {
          top = top2;
        }
        else
        {
          top = top1;
        }
 
        left = i*fWidth;
 
        g.DrawString(code.Substring(i, 1), f, b, left, top);
      }
 
      //画一个边框 边框颜色为Color.Gainsboro
      g.DrawRectangle(new Pen(Color.Gainsboro, 0), 0, 0, image.Width - 1, image.Height - 1);
      g.Dispose();
 
      //产生波形
      //image = TwistImage(image, true, 8, 4);
 
      return image;
    }
 
    #endregion
 
    //将创建好的图片输出到页面
    public void CreateImageOnPage(string code, HttpContext context)
    {
      System.IO.MemoryStream ms = new System.IO.MemoryStream();
      Bitmap image = this.CreateImageCode(code);
 
      image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
 
      context.Response.ClearContent();
      context.Response.ContentType = "image/Jpeg";
      context.Response.BinaryWrite(ms.GetBuffer());
 
      ms.Close();
      ms = null;
      image.Dispose();
      image = null;
    }
 
    #endregion
 
    #region 生成随机字符码
 
    public string CreateVerifyCode(int codeLen)
    {
      if (codeLen == 0)
      {
        codeLen = Length;
      }
 
      string[] arr = CodeSerial.Split(',');
 
      string code = "";
 
      int randValue = -1;
 
      Random rand = new Random(unchecked((int) DateTime.Now.Ticks));
 
      for (int i = 0; i < codeLen; i++)
      {
        randValue = rand.Next(0, arr.Length - 1);
 
        code += arr[randValue];
      }
 
      return code;
    }
 
    public string CreateVerifyCode()
    {
      return CreateVerifyCode(0);
    }
 
    #endregion
  }
}

是不是很有成就感,为大家分享的代码很完整,大家可以在这个基础进行扩展,希望大家喜欢。