• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

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

asp 搞代码 4年前 (2022-01-03) 40次浏览 已收录 0个评论

在.NET中新建一个Web项目,添加一个.ASPX页面,取名VerifyCode.aspx, 转到其代码编辑状态,将下面的代码Copy&Paste过去就可以用了,有什么意见或问题欢迎提出^@^

验证码技术是为了防止暴力破解等而设定的。现在一般的网站注册等都提供验证码功能,特别是腾讯更是长长的一串。文中参考了别人的代码。有了就没有必要再写了。可以读一下。不过我测试时发现了两次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;
/**/////

/// 页面验证码程序
/// 使用:在页面中加入HTML代码
///

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);
}
/**//**//**////

/// Required method for Designer support – do not modify
/// the contents of this method with the code editor.
///

private void InitializeComponent()
…{
//this.Load += new System.EventHandler(this.Page_Load);
}
/**////

///
///

///
///
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();
this.OnPaint();
}
}
/**//**//**////

/// 设置页面不被缓存
///

private void SetPageNoCache()
…{
Response.Buffer = true;
Response.ExpiresAbsolute = System.DateTime.Now.AddSeconds(-1);
Response.Expires = 0;
Response.CacheControl = “no-cache”;
Response.AppendHeader(“Pragma”,”No-Cache”);
}
/**//**//**////

/// 取得一个 4 位的随机码
///

///
private string GetRandomCode()
…{
return Guid.NewGuid().ToString().Substring(0, 4);
}
/**//**//**////

/// 随机取一个字体
///

///
private Font GetFont()
…{
int fontIndex = _random.Next(0, FontItems.Length);
FontStyle fontStyle = GetFontStyle(_random.Next(0, 2));
return new Font(FontItems[fontIndex], 12, fontStyle);
}
/**//**//**////

/// 取一个字体的样式
///

///
///
private FontStyle GetFontStyle(int index)
…{
switch (index)
…{
case 0:
return FontStyle.Bold;
case 1:
return FontStyle.Italic;
default:
return FontStyle.Regular;
}
}
/**//**//**////

/// 随机取一个笔刷
///

///
private Brush GetBrush()
…{
int brushIndex = _random.Next(0, BrushItems.Length);
_brushNameIndex = brushIndex;
return BrushItems[brushIndex];
}
/**//**//**////

/// 绘画事件
///

private void OnPaint()
…{
Bitmap objBitmap = null;
Graphics g = null;
try
…{
objBi来源[email protected]搞@^&代*@码网tmap = new Bitmap(Width, Height);
g = Graphics.FromImage(objBitmap);
Paint_Background(g);
Paint_Text(g);
Paint_TextStain(objBitmap);
Paint_Border(g);
objBitmap.Save(Response.OutputStream, ImageFormat.Gif);
Response.ContentType = “image/gif”;
}
catch …{}
finally
…{
if (null != objBitmap)
objBitmap.Dispose();
if (null != g)
g.Dispose();
}
}
/**//**//**////

/// 绘画背景颜色
///

///
private void Paint_Background(Graphics g)
…{
g.Clear(BackColor);
}
/**//**//**////

/// 绘画边框
///

///
private void Paint_Border(Graphics g)
…{
g.DrawRectangle(BorderColor, 0, 0, Width – 1, Height – 1);
}
/**//**//**////

/// 绘画文字
///

///
private void Paint_Text(Graphics g)
…{
g.DrawString(_code, GetFont(), GetBrush(), 3, 1);
}
/**//**//**////

/// 绘画文字噪音点
///

///
private void Paint_TextStain(Bitmap b)
…{
for (int n=0; n<30; n++)
…{
int x = _random.Next(Width);
int y = _random.Next(Height);
b.SetPixel(x, y, Color.FromName(BrushName[_brushNameIndex]));
}
}
}

2 页面引用:

一般需要同时提供刷新功能(看不清楚换一张),代码如下
  刷新验证码
如使用了母版页,则用如下代码:
 
  刷新验证码
超链接对应的javascript如下:

3 判断验证
上述代码是将验证码存贮在Session中,用code来标志。读取代码Session[“code”].ToString();
使用中,我们只需要比较Session[“code”].ToString()和文本框输入的串(TextBoxCode.Text)是否相同即可进行判断。
if(Session[“code”].ToString().Trim().Equals(TextBoxCode.Text.Trim()))
…{
Response.Write(“Success”);
}
测试通过! 

以上就是asp.net 验证码生成和刷新及验证的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:asp.net 验证码生成和刷新及验证

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址