图片验证在第一次登陆的时候,明明输入正确,却还提示验证码错误,在第二次输入就没有这个问题了

dnvvj 2008-08-05 08:29:35

//图片验证在第一次登陆的时候,明明输入正确,却还提示验证码错误,在第二次输入就没有这个问题了





protected void Page_Load(object sender, EventArgs e)
{
//读取图片验证码程序-----------------------
this.btnimage.ImageUrl = "TextVerify.aspx";
}





protected void Page_Load(object sender, EventArgs e)
{
this.GenImg(this.GenCode(4));
////将验证码存储到session中,以便需要时进行验证
Session["image"] = this.GenCode(4);
}
//任意产生4个验证码
private string GenCode(int num)
{
//定义一个验证码数组
string[] source ={ "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 code = "";
Random rd = new Random();
for (int i = 0; i < num; i++)
{
code += source[rd.Next(0, source.Length)];
}
return code;
}
//生成图片
private void GenImg(string code)
{
//定义一个画板
Bitmap myPalette = new Bitmap(50, 18);
//在画板上定义绘图的实例
Graphics gh = Graphics.FromImage(myPalette);
//定义一个矩形
Rectangle rc = new Rectangle(0, 0, 50, 18);
//填充矩形
gh.FillRectangle(new SolidBrush(Color.Black), rc);
//在矩形内画出字符串
gh.DrawString(code, new Font("Arial", 11), new SolidBrush(Color.White), rc);
//将图片显示出来
myPalette.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
gh.Dispose();
myPalette.Dispose();
}





我在两个程序里都加过if(!ispostback) 怀疑是这里的问题。可是加上也不行啊。
...全文
1895 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
athonyho 2011-09-08
  • 打赏
  • 举报
回复
如今我也遇到这样的问题,但有新的发现。现在我用的是64位系统 IE 9 和 搜狗浏览器,发现在使用 是兼容模式,和搜狗的高速方式 是正常的,反之在 注销退出 比如运行了 Session.Abandon(); 后就要刷新两次验证码登录才正确。
wdgphc 2008-08-05
  • 打赏
  • 举报
回复
我还以为你说CSDN呢,我就经常这样.帮顶!
DshirenJ 2008-08-05
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 newtypebao 的回复:]
在page_load中加上if (!Ispostback)
[/Quote]
newtypebao 2008-08-05
  • 打赏
  • 举报
回复
在page_load中加上if (!Ispostback)
sxmonsy 2008-08-05
  • 打赏
  • 举报
回复
你生成了二回验证码:

protected void Page_Load(object sender, EventArgs e)
{
this.GenImg(this.GenCode(4));
Session["image"] = this.GenCode(4);
}

改成这样:

protected void Page_Load(object sender, EventArgs e)
{
string code = this.GenCode(4);
this.GenImg(code);
Session["image"] = code ;
}

wszhoho 2008-08-05
  • 打赏
  • 举报
回复
protected void Page_Load(object sender, EventArgs e)
{
if (!Ispostback)
{

this.GenImg(this.GenCode(4));
////将验证码存储到session中,以便需要时进行验证
Session["image"] = this.GenCode(4);
}
}

这样的,提交的时候会刷新一次,然后session就变成新的了,并不是上次填写的。
ljqingas 2008-08-05
  • 打赏
  • 举报
回复
protected void Page_Load(object sender, EventArgs e)
{
if (!Ispostback)
{
this.GenImg(this.GenCode(4));
////将验证码存储到session中,以便需要时进行验证
Session["image"] = this.GenCode(4);
}
}
这样就可以了啊!
你原来那的话,Session["image"] = this.GenCode(4);
Session里面的值是上次的值。
pcb_ghl 2008-08-05
  • 打赏
  • 举报
回复

private string GenCode(int num)
{
//定义一个验证码数组
string[] source ={ "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 code = "";
Random rd = new Random();
for (int i = 0; i < num; i++)
{
code += source[rd.Next(0, source.Length)];
}
///把session放在这里
Session["image"] = code;
return code;
}

pcb_ghl 2008-08-05
  • 打赏
  • 举报
回复

private string GenCode(int num)
{
//定义一个验证码数组
string[] source ={ "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 code = "";
Random rd = new Random();
for (int i = 0; i < num; i++)
{
code += source[rd.Next(0, source.Length)];
}

///把session放在这里
Session["image"] = this.GenCode(4);

return code;
}

suyiming 2008-08-05
  • 打赏
  • 举报
回复
加上
if (Page.IsValid)
{
...........

}
shadowjl 2008-08-05
  • 打赏
  • 举报
回复
up
system_009 2008-08-05
  • 打赏
  • 举报
回复
up
qian6688099 2008-08-05
  • 打赏
  • 举报
回复
很明显你两次获得的验证码不是一样的。

可能是页面第一次加载的关系,你用ispostback来判断一下试试可以否?
sxlfybb 2008-08-05
  • 打赏
  • 举报
回复
雷同,巧合。

sxlfybb 2008-08-05
  • 打赏
  • 举报
回复
protected void Page_Load(object sender, EventArgs e)
{
this.GenImg(this.GenCode(4));
////将验证码存储到session中,以便需要时进行验证
Session["image"] = this.GenCode(4);
}


生成图片的时候生成了一次验证码,在session里面又生成了一次验证码。

修改为:

protected void Page_Load(object sender, EventArgs e)
{
string code = this.GenCode(4);
this.GenImg(code);
////将验证码存储到session中,以便需要时进行验证
Session["image"] = code ;
}
chinahnzl 2008-08-05
  • 打赏
  • 举报
回复
{
this.GenImg(this.GenCode(4));
////将验证码存储到session中,以便需要时进行验证
Session["image"] = this.GenCode(4);
}


两次获得的不一样吧。。

string rndCode = this.GenCode(4);
this.GetImg(rndCode);
Session["image"] = rndCode;


fengboawhf 2008-08-05
  • 打赏
  • 举报
回复
ding
liukang4098 2008-08-05
  • 打赏
  • 举报
回复
up

62,067

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧