protected void Page_Load(object sender, EventArgs e)
{
//获取6位随机数
string str = getRand(6);
//将验证码写入Session,进行验证。
Session["check"] = str;
//生成图像的
getImageValidate(str);
}
问题:当打开index.aspx页面时验证码的Session是空的,当刷新一下页面再去验证时Session里面是有值,但是值是上一次验证码的。 各位大哥大姐看看哈。
6 个解决方案
#1
给自己顶一下
#2
#3
你给出的这个代码也看不出什么问题啊!
#4
用我这个看看
protected void Page_Load(object sender, EventArgs e)
{
CreateCheckCodeImage(CreateCode());
}
public string CreateCode()
{
string so = "1,2,3,4,5,6,7,8,9,0";
string[] strArr = so.Split(',');
string code = "";
Random rand = new Random();
for (int i = 0; i < 4; i++)
{
code += strArr[rand.Next(0, strArr.Length)];
}
Response.Cookies.Add(new HttpCookie("validate", code));
return code;
}
private void CreateCheckCodeImage(string validata)
{ //若校验码为空,则直接返回
if (validata == null || validata.Trim() == String.Empty)
return;
//根据校验码的长度确定输出图片的长度
System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling(Convert.ToDecimal(validata.Length * 15)), 20);
//创建Graphics对象
Graphics g = Graphics.FromImage(image);
try
{
//清空图片背景色
g.Clear(Color.White);
//画图片的背景噪音线
Random random = new Random();
for (int i = 0; i < 10; i++)
{
//噪音线起点坐标(x1,y1),终点坐标(x2,y2)
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 Font("Arial", 12, (FontStyle.Bold | FontStyle.Italic));
//线性渐变画刷
LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.Purple, 1.2f, true);
g.DrawString(validata, font, brush, 2, 2);
//画图片的前景噪音点
for (int i = 0; i < 50; 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.SaddleBrown), 0, 0, image.Width - 1, image.Height - 1);
//创建内存流用于输出图片,这里的MemoryStream会在使用后就被Dispose()
using (MemoryStream ms = new MemoryStream())
{ //图片格式指定为png
image.Save(ms, ImageFormat.Png);
//清除缓冲区流中的所有输出
Response.ClearContent();
//输出流的HHT MIME类型设置为"image/png"
Response.ContentType = "image/Png";
//输出的图片的二进制流
Response.BinaryWrite(ms.ToArray());
}
}
finally
{ //释放Bitmap对象和Graphics对象
g.Dispose();
image.Dispose();
}
}
#5
防止请求有页级缓存,在连接后面加一个参数,动态赋值,比如...aspx?pd=(new Date().getDate()),或者Math.random();
#6
给自己顶一个
哈哈
#1
给自己顶一下
#2
Refer this:
http://www.cnblogs.com/insus/articles/1425377.html
http://www.cnblogs.com/insus/articles/1945539.html
http://www.cnblogs.com/insus/articles/1425377.html
http://www.cnblogs.com/insus/articles/1945539.html
#3
你给出的这个代码也看不出什么问题啊!
#4
用我这个看看
protected void Page_Load(object sender, EventArgs e)
{
CreateCheckCodeImage(CreateCode());
}
public string CreateCode()
{
string so = "1,2,3,4,5,6,7,8,9,0";
string[] strArr = so.Split(',');
string code = "";
Random rand = new Random();
for (int i = 0; i < 4; i++)
{
code += strArr[rand.Next(0, strArr.Length)];
}
Response.Cookies.Add(new HttpCookie("validate", code));
return code;
}
private void CreateCheckCodeImage(string validata)
{ //若校验码为空,则直接返回
if (validata == null || validata.Trim() == String.Empty)
return;
//根据校验码的长度确定输出图片的长度
System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling(Convert.ToDecimal(validata.Length * 15)), 20);
//创建Graphics对象
Graphics g = Graphics.FromImage(image);
try
{
//清空图片背景色
g.Clear(Color.White);
//画图片的背景噪音线
Random random = new Random();
for (int i = 0; i < 10; i++)
{
//噪音线起点坐标(x1,y1),终点坐标(x2,y2)
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 Font("Arial", 12, (FontStyle.Bold | FontStyle.Italic));
//线性渐变画刷
LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.Purple, 1.2f, true);
g.DrawString(validata, font, brush, 2, 2);
//画图片的前景噪音点
for (int i = 0; i < 50; 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.SaddleBrown), 0, 0, image.Width - 1, image.Height - 1);
//创建内存流用于输出图片,这里的MemoryStream会在使用后就被Dispose()
using (MemoryStream ms = new MemoryStream())
{ //图片格式指定为png
image.Save(ms, ImageFormat.Png);
//清除缓冲区流中的所有输出
Response.ClearContent();
//输出流的HHT MIME类型设置为"image/png"
Response.ContentType = "image/Png";
//输出的图片的二进制流
Response.BinaryWrite(ms.ToArray());
}
}
finally
{ //释放Bitmap对象和Graphics对象
g.Dispose();
image.Dispose();
}
}
#5
防止请求有页级缓存,在连接后面加一个参数,动态赋值,比如...aspx?pd=(new Date().getDate()),或者Math.random();
#6
给自己顶一个
哈哈