服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服务器之家 - 编程语言 - ASP.NET教程 - asp.net登录验证码实现方法

asp.net登录验证码实现方法

2020-03-22 17:36学习虾 ASP.NET教程

这篇文章主要为大家详细介绍了asp.net登录验证码实现方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

前端添加的标签和方法:
验证码:

复制代码 代码如下:
<input id="txtVerifyCode" type="text" maxlength="5" style="line-height: 30px;  height: 30px; width: 80px;border:solid 1px #d4d4d4;" class="input"/> <img src="" target='_blank'>图片刷新</p>//标签

 

 

?
1
2
3
4
5
6
7
8
9
10
11
$(function () {
      $("#imgValidateCode").click(function () {
        DoFresh();
      });
      DoFresh();
    })
function DoFresh() {
      var img = $("#imgValidateCode");
      img.attr("src", "VerifyCode.aspx?random=" + Math.random());
 
    } //添加的方法,src是生成图片的aspx的地址

然后在项目中在新建一个VerifyCode.aspx,下面是aspx的代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="VerifyCode.aspx.cs" Inherits="Form.VerifyCode" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
 <head id="Head1" runat="server">
 
   <title></title>
 
 </head>
 <body>
   <form id="form1" runat="server">
   <div>
   
  </div>
   </form>
 
 </body>
 </html>

接着是aspx.cs的代码:

?
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
using System.Drawing;
 
 
 
namespace Form
{
  public partial class VerifyCode : System.Web.UI.Page
  {
    public static string HZ;
    /// <summary>
    /// 验证码的最大长度
    /// </summary>
    public int MaxLength
    {
      get { return 10; }
 
    }
    /// <summary>
    /// 验证码的最小长度
    /// </summary>
    public int MinLength
    {
      get { return 1; }
    }
 
    protected void Page_Load(object sender, EventArgs e)
    {
      string[] str = CreateValidateNumber(4);
      string strcode = string.Empty;
      for (int i = 0; i < str.Length; i++)
      {
      strcode += str[i];
 
      }
    CreateCheckCodeImage(str);
      HZ = strcode;
      Response.Write(HZ);
     //验证码存入session
      Session["ValidateCode"] = HZ;
    }
 
    /// <summary>
    /// 生成验证码
    /// </summary>
    /// <param name="length">指定验证码的长度</param>
    /// <returns>验证码</returns>
    public string[] CreateValidateNumber(int length)
 
    {
      string Vchar = "1,2,3,4,5,6,7,8,9,a,b,c,d,e,f,g,h,i,j,k,l,m,n,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,P,Q" +
      ",R,S,T,U,V,W,X,Y,Z";
 
      string[] VcArray = Vchar.Split(new Char[] { ',' });//拆分成数组
       string[] num = new string[length];
 
      int temp = -1;//记录上次随机数值,尽量避避免生产几个一样的随机数
 
     Random rand = new Random();
 
      //采用一个简单的算法以保证生成随机数的不同
 
      for (int i = 1; i < length + 1; i++)
 
      {
 
        if (temp != -1)
 
        {
 
          rand = new Random(i * temp * unchecked((int)DateTime.Now.Ticks));
 
        }
        int t = rand.Next(VcArray.Length-1);
        if (temp != -1 && temp == t)
        {
          return CreateValidateNumber(length);
 
 
 
        }
 
        temp = t;
        num[i - 1] = VcArray[t];
        //num.SetValue(VcArray[t]);
        //VNum += VcArray[t];
 
      }
      return num;
    }
 
   private void CreateCheckCodeImage(string[] checkCode)
    {
      if (checkCode == null || checkCode.Length <= 0)
        return;
      System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 32.5)), 60);
      System.Drawing.Graphics g = Graphics.FromImage(image);
     try
      {
       //生成随机生成器
 
        Random random = new Random();
 
       //清空图片背景色
 
        g.Clear(Color.White);
 
       //定义颜色
 
        Color[] c = { Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Orange, Color.Brown, Color.DarkCyan, Color.Purple };
 
        //画图片的背景噪音线
        for (int i = 0; i < 25; i++)
 
        {
          int cindex = random.Next(7);
          int findex = random.Next(5);
          int x1 = random.Next(image.Width);
          int x2 = random.Next(image.Width);
          int y1 = random.Next(image.Height);
          int y2 = random.Next(image.Height);
 
          // g.DrawLine(new Pen(c[cindex]), x1, y1, x2, y2);
        }
        //定义字体
        string[] f = { "Verdana", "Microsoft Sans Serif", "Comic Sans MS", "Arial", "宋体" };
 
       for (int k = 0; k <= checkCode.Length - 1; k++)
        {
          int cindex = random.Next(7);
          int findex = random.Next(5);
          Font drawFont = new Font(f[findex], 26, (System.Drawing.FontStyle.Bold));
          SolidBrush drawBrush = new SolidBrush(c[cindex]);
          float x = 5.0F;
          float y = 0.0F;
          float width = 42.0F;
          float height = 48.0F;
          int sjx = random.Next(10);
          int sjy = random.Next(image.Height - (int)height);
 
          RectangleF drawRect = new RectangleF(x + sjx + (k * 25), y + sjy, width, height);
          StringFormat drawFormat = new StringFormat();
          drawFormat.Alignment = StringAlignment.Center;
        g.DrawString(checkCode[k], drawFont, drawBrush, drawRect, drawFormat);
        }
        //画图片的前景噪音点
        for (int i = 0; i < 500; i++)
        {
          int x = random.Next(image.Width);
          int y = random.Next(image.Height);
 
          image.SetPixel(x, y, Color.FromArgb(random.Next()));
 
        }
 
        int cindex1 = random.Next(7);
 
        //画图片的边框线
 
        g.DrawRectangle(new Pen(c[cindex1]), 0, 0, image.Width - 1, image.Height - 1);
       System.IO.MemoryStream ms = new System.IO.MemoryStream();
        image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
        Response.ClearContent();
        Response.ContentType = "image/Gif";
        Response.BinaryWrite(ms.ToArray());
 
      }
 
      finally
 
      {
 
        g.Dispose();
 
        image.Dispose();
 
      }
 
    }
 
  }
 
}

于是!就可以生成验证码了,然后自己再把编写验证版的判断逻辑写好就可以啦!

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

延伸 · 阅读

精彩推荐