ASP.net 验证码实现代码(C#)
程序员文章站
2023-03-13 16:59:49
public class validatecode : system.web.ui.page { priva...
public class validatecode : system.web.ui.page
{
private void page_load(object sender, system.eventargs e)
{
this.createcheckcodeimage(generatecheckcode());
}
#region web 窗体设计器生成的代码
override protected void oninit(eventargs e)
{
//
// codegen: 该调用是 asp.net web 窗体设计器所必需的。
//
initializecomponent();
base.oninit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void initializecomponent()
{
this.load += new system.eventhandler(this.page_load);
}
#endregion
private string generatecheckcode()
{
int number;
char code;
string checkcode = string.empty;
system.random random = new random();
for(int i=0; i<5; i++)
{
number = random.next();
if(number % 2 == 0)
code = (char)('0' + (char)(number % 10));
else
code = (char)('a' + (char)(number % 26));
checkcode += code.tostring();
}
response.cookies.add(new httpcookie("checkcode", checkcode));
return checkcode;
}
private void createcheckcodeimage(string checkcode)
{
if(checkcode == null || checkcode.trim() == string.empty)
return;
system.drawing.bitmap image = new system.drawing.bitmap((int)math.ceiling((checkcode.length * 12.5)), 22);
graphics g = graphics.fromimage(image);
try
{
//生成随机生成器
random random = new random();
//清空图片背景色
g.clear(color.white);
//画图片的背景噪音线
for(int i=0; i<25; i++)
{
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(color.silver), x1, y1, x2, y2);
}
font font = new system.drawing.font("arial", 12, (system.drawing.fontstyle.bold | system.drawing.fontstyle.italic));
system.drawing.drawing2d.lineargradientbrush brush = new system.drawing.drawing2d.lineargradientbrush(new rectangle(0, 0, image.width, image.height), color.blue, color.darkred, 1.2f, true);
g.drawstring(checkcode, font, brush, 2, 2);
//画图片的前景噪音点
for(int i=0; i<100; i++)
{
int x = random.next(image.width);
int y = random.next(image.height);
image.setpixel(x, y, color.fromargb(random.next()));
}
//画图片的边框线
g.drawrectangle(new pen(color.silver), 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.c;
response.binarywrite(ms.toarray());
}
finally
{
g.dispose();
image.dispose();
}
}
}
假如以上验证码生成器页面名为:checkcode.aspx,那么在登录页面中使用“<img>” 这个 html 元素来显示生成的验证码图片:<img src="checkcode.aspx">
在登录页面的登录按钮的处理事件中使用以下代码判断验证码:
private void btnlogin_click(object sender, system.web.ui.imageclickeventargs e)
{
if(request.cookies["checkcode"] == null)
{
lblmessage.text = "您的浏览器设置已被禁用 cookies,您必须设置浏览器允许使用 cookies 选项后才能使用本系统。";
lblmessage.visible = true;
return;
}
if(string.compare(request.cookies["checkcode"].value, txtcheckcode.text, true) != 0)
{
lblmessage.text = "验证码错误,请输入正确的验证码。";
lblmessage.visible = true;
return;
}
}
{
private void page_load(object sender, system.eventargs e)
{
this.createcheckcodeimage(generatecheckcode());
}
#region web 窗体设计器生成的代码
override protected void oninit(eventargs e)
{
//
// codegen: 该调用是 asp.net web 窗体设计器所必需的。
//
initializecomponent();
base.oninit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void initializecomponent()
{
this.load += new system.eventhandler(this.page_load);
}
#endregion
private string generatecheckcode()
{
int number;
char code;
string checkcode = string.empty;
system.random random = new random();
for(int i=0; i<5; i++)
{
number = random.next();
if(number % 2 == 0)
code = (char)('0' + (char)(number % 10));
else
code = (char)('a' + (char)(number % 26));
checkcode += code.tostring();
}
response.cookies.add(new httpcookie("checkcode", checkcode));
return checkcode;
}
private void createcheckcodeimage(string checkcode)
{
if(checkcode == null || checkcode.trim() == string.empty)
return;
system.drawing.bitmap image = new system.drawing.bitmap((int)math.ceiling((checkcode.length * 12.5)), 22);
graphics g = graphics.fromimage(image);
try
{
//生成随机生成器
random random = new random();
//清空图片背景色
g.clear(color.white);
//画图片的背景噪音线
for(int i=0; i<25; i++)
{
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(color.silver), x1, y1, x2, y2);
}
font font = new system.drawing.font("arial", 12, (system.drawing.fontstyle.bold | system.drawing.fontstyle.italic));
system.drawing.drawing2d.lineargradientbrush brush = new system.drawing.drawing2d.lineargradientbrush(new rectangle(0, 0, image.width, image.height), color.blue, color.darkred, 1.2f, true);
g.drawstring(checkcode, font, brush, 2, 2);
//画图片的前景噪音点
for(int i=0; i<100; i++)
{
int x = random.next(image.width);
int y = random.next(image.height);
image.setpixel(x, y, color.fromargb(random.next()));
}
//画图片的边框线
g.drawrectangle(new pen(color.silver), 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.c;
response.binarywrite(ms.toarray());
}
finally
{
g.dispose();
image.dispose();
}
}
}
假如以上验证码生成器页面名为:checkcode.aspx,那么在登录页面中使用“<img>” 这个 html 元素来显示生成的验证码图片:<img src="checkcode.aspx">
在登录页面的登录按钮的处理事件中使用以下代码判断验证码:
private void btnlogin_click(object sender, system.web.ui.imageclickeventargs e)
{
if(request.cookies["checkcode"] == null)
{
lblmessage.text = "您的浏览器设置已被禁用 cookies,您必须设置浏览器允许使用 cookies 选项后才能使用本系统。";
lblmessage.visible = true;
return;
}
if(string.compare(request.cookies["checkcode"].value, txtcheckcode.text, true) != 0)
{
lblmessage.text = "验证码错误,请输入正确的验证码。";
lblmessage.visible = true;
return;
}
}