在网上找了一个验证码的源代码,使用后出现如下问题:不管你输入正确与否,都会提示验证码错误。后来经过在qq群里和csdn发贴咨询,得知是ie浏览器缓存问题。在经过他人的指点后改正了错误。源代码如下:
-------------------validate.aspx------------------
<%@ page language="c#" %>
<%@ import namespace="system"%>
<%@ import namespace="system.io"%>
<%@ import namespace="system.drawing"%>
<%@ import namespace="system.drawing.imaging"%>
<%@ import namespace="system.drawing.drawing2d"%>
<script runat="server">
private bitmap validateimage;
private graphics g;
public void page_load(object sender ,eventargs e ){
response.bufferoutput = true; //特别注意
response.cache.setexpires(datetime.now.addmilliseconds(-1));//特别注意
response.cache.setcacheability(httpcacheability.nocache);//特别注意
response.appendheader("pragma", "no-cache"); //特别注意
string vnum =makevalidatecode( );
session["vnum"]=vnum;//取得验证码,以便后来验证
validatecode(vnum);}
public void validatecode(string vnum)
{
validateimage = new bitmap(60, 20, pixelformat.format24bpprgb);
g = graphics.fromimage(validateimage);
g.fillrectangle(new lineargradientbrush(new point(0,0), new point(110,20), color.fromargb(240,255,255,255),color.fromargb(240,255,255,255)),0,0,200,200);
g.drawstring(vnum, new font("arial",11),new solidbrush(color.red),new pointf(6,0));
g.save();
memorystream ms=new memorystream();
validateimage.save(ms,system.drawing.imaging.imageformat.gif);
response.clearcontent();
response.contenttype="image/bmp";
response.binarywrite(ms.toarray());
response.end();
}
string makevalidatecode()
{
char[] s = new char[]{'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'};
string num = "";
random r = new random();
for(int i = 0; i < 5; i++)
{
num += s[r.next(0, s.length)].tostring();
}
return num;
}
</script>
-----------------------register.aspx------------
<%@ page language="c#" autoeventwireup="true" codefile="register.aspx.cs" inherits="myregister.register" %>
<!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 runat="server">
<title>会员注册</title>
<link href="stylesheet.css" _fcksavedurl=""stylesheet.css"" _fcksavedurl=""stylesheet.css"" _fcksavedurl=""stylesheet.css"" type="text/css" rel="stylesheet">
</head>
<body>
<form id="form1" runat="server">
<div class="middle" >
<div >
<asp:label id="username" runat="server" text="用户名:" ></asp:label>
<asp:textbox id="textbox1" runat="server"></asp:textbox>
</div>
<div >
<asp:label id="userpsw" runat="server" text="密码:" ></asp:label>
<asp:textbox id="textbox2" runat="server"></asp:textbox></div>
<div >
<asp:label id="label2" runat="server" text="确认密码:" ></asp:label>
<asp:textbox id="textbox6" runat="server"></asp:textbox></div>
<div >
<asp:label id="question" runat="server" text="找回密码问题:"></asp:label>
<asp:textbox id="textbox3" runat="server"></asp:textbox></div>
<div>
<asp:label id="answer" runat="server" text="找回密码答案:"></asp:label>
<asp:textbox id="textbox4" runat="server"></asp:textbox></div>
<div >
<asp:label id="email" runat="server" text="电子邮件:"></asp:label>
<asp:textbox id="textbox5" runat="server"></asp:textbox></div>
<div >
<asp:label id="label1" runat="server" text="验证码:"></asp:label>
<asp:textbox id="checkcode" runat="server" width="85px"></asp:textbox>
<asp:image id="image1" runat="server" imageurl="validate.aspx"></asp:image></div>
<div>
<asp:button id="button1" runat="server" text="确定" onclick="button1_click" /></div>
<div class="text">
<asp:label id="message" runat="server" ></asp:label></div>
</div>
</form>
</body>
</html>
-------------------------register.aspx.cs-------------------
using system;
using system.data;
using system.configuration;
using system.collections;
using system.web;
using system.web.security;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.webcontrols.webparts;
using system.web.ui.htmlcontrols;
namespace myregister
{
public partial class register : system.web.ui.page
{
protected void page_load(object sender, eventargs e)
{
}
protected void button1_click(object sender, eventargs e)
{
string checkcode = checkcode.text;
//response.write(session["vnum"]);
if (checkcode == session["vnum"].tostring() || session["vnum"].tostring()==null)//注意session["vnum"].tostring(),必须加上tostring(),因//为session["vnum"]是对象。
response.redirect ("default.aspx");
else
message.text = "验证码错误或为空!";
}
}
}
---------------------------------
其中注释的地方应特别注意
新闻热点
疑难解答