session[]传递验证码

very08 2010-05-25 03:31:02
我的验证码使用Session传值
验证码图片src=VerifyCode.aspx

现在的问题是当用户登录后session过期后
再点击会员页面跳转到登录页面后 第一登录失败

这时Session里验证码是空
验证码图片没有更新

Random rand = new Random();
imgVerifyCode.ImageUrl = "VerifyCode.aspx?" + rand.Next(100).ToString();

验证码图片地址改成这样也试过
跳转之后第一次登录还是不成功 应该这么弄啊 谢谢大家先
...全文
156 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
小超超 2011-12-16
  • 打赏
  • 举报
回复
我也遇到这个问题了,不知道楼主解决了没有???
myhope88 2010-05-25
  • 打赏
  • 举报
回复
大家都说得详细,不用多说了,路过
h123hu 2010-05-25
  • 打赏
  • 举报
回复
当用户跳回登陆页面时,直接刷新一下验证码不行吗?

写一个更新验证码和图片的方法
mngzilin 2010-05-25
  • 打赏
  • 举报
回复
//if (!IsPostBack)去掉
{

Response.Expires = 0;
Response.CacheControl = "no-cache";
Session["code"] = GenerateCheckCode();
this.CreateCheckCodeImage(Session["code"].ToString());
}
very08 2010-05-25
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 vip__888 的回复:]
是你的缓存问题
[/Quote]
什么问题啊
very08 2010-05-25
  • 打赏
  • 举报
回复
VerifyCode.aspx
string code = CreateVerifyCode(4);
Session["VerifyCode"] = code;
CreateImageOnPage(code);

生成时给session赋值了
我想问的是session过期了之后
vip__888 2010-05-25
  • 打赏
  • 举报
回复
是你的缓存问题
qq497525725 2010-05-25
  • 打赏
  • 举报
回复
VerifyCode.aspx 这个页面生成的时候应该给Seesion赋值
very08 2010-05-25
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 f800051235 的回复:]
你生成验证码的页面代码有问题吧。
自己去断点看下你存验证码的SESSION里面保存的东西跟显示的是不是一致。
[/Quote]
验证码一直好用
只有当session过期后 第一次用时 session是空 不好使 之后就没问题了
a12321321321312321 2010-05-25
  • 打赏
  • 举报
回复
给段生成验证码的代码你参考下,验证码保存在Session["code"] 里面。

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Drawing;

public partial class CheckCodeImage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{

Response.Expires = 0;
Response.CacheControl = "no-cache";
Session["code"] = GenerateCheckCode();
this.CreateCheckCodeImage(Session["code"].ToString());
}
}

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(0, 99);

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.ContentType = "image/Gif";
Response.BinaryWrite(ms.ToArray());
}
finally
{
g.Dispose();
image.Dispose();
}
}

}

a12321321321312321 2010-05-25
  • 打赏
  • 举报
回复
你生成验证码的页面代码有问题吧。
自己去断点看下你存验证码的SESSION里面保存的东西跟显示的是不是一致。

62,074

社区成员

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

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

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

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