首页 > 开发 > .Net > 正文

asp.net 验证码生成和刷新及验证

2020-04-24 22:11:54
字体:
来源:转载
供稿:网友
验证码技术是为了防止暴力破解等而设定的。现在一般的网站注册等都提供验证码功能,特别是腾讯更是长长的一串。文中参考了别人的代码。有了就没有必要再写了。可以读一下。不过我测试时发现了两次PageLoad的问题。注释了两句即可。同时修改了namespaces。同时提供完整的验证说明:
1 新建VerifyCode.aspx
cs文件代码如下:
代码如下:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Text;
/**///// <summary>
/// 页面验证码程序
/// 使用:在页面中加入HTML代码 <img src="VerifyCode.aspx">
/// </summary>
public partial class VerifyCode : System.Web.UI.Page
...{
static string[] FontItems = new string[] ...{ "Arial",
"Helvetica",
"Geneva",
"sans-serif",
"Verdana"
};
static Brush[] BrushItems = new Brush[] ...{ Brushes.OliveDrab,
Brushes.ForestGreen,
Brushes.DarkCyan,
Brushes.LightSlateGray,
Brushes.RoyalBlue,
Brushes.SlateBlue,
Brushes.DarkViolet,
Brushes.MediumVioletRed,
Brushes.IndianRed,
Brushes.Firebrick,
Brushes.Chocolate,
Brushes.Peru,
Brushes.Goldenrod
};
static string[] BrushName = new string[] ...{ "OliveDrab",
"ForestGreen",
"DarkCyan",
"LightSlateGray",
"RoyalBlue",
"SlateBlue",
"DarkViolet",
"MediumVioletRed",
"IndianRed",
"Firebrick",
"Chocolate",
"Peru",
"Goldenrod"
};
private static Color BackColor = Color.White;
private static Pen BorderColor = Pens.DarkGray;
private static int Width = 52;
private static int Height = 21;
private Random _random;
private string _code;
private int _brushNameIndex;
override protected void OnInit(EventArgs e)
...{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
//InitializeComponent();
//base.OnInit(e);
}
/**//**//**//// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
...{
//this.Load += new System.EventHandler(this.Page_Load);
}
/**//// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public void Page_Load(object sender, System.EventArgs e)
...{
if (!IsPostBack)
...{
//
// TODO : initialize
//
this._random = new Random();
this._code = GetRandomCode();
//
// TODO : use Session["code"] save the VerifyCode
//
Session["code"] = this._code;
//
// TODO : output Image
//
this.SetPageNoCache();
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表