日期:2014-05-20  浏览次数:20512 次

关于Session问题
做图片验证码。会用SESSION存取随机数。可第一次Session总为空,验证码有显示
每次第一次Session就会为空。郁闷半天。求助了。

代码如下:
图片验证码:
if   (!IsPostBack)  
{  
MakeValidateCode()   ;
}  
void   MakeValidateCode()  
{  
char[]   s   =   new   char[]{ '1 ',   '2 ', '3 ', '4 ', '5 ', '6 ', '7 ', '8 ', '9 '};  
string   num   =   " ";  
Random   r   =   new   Random();  
for(int   i   =   0;   i   <   5;   i++)  
{  
num   +=   s[r.Next(0,   s.Length)].ToString();  
}  
ValidateCode(num);
}  

public   void   ValidateCode(string   VNum)  
{  
validateimage   =   new   Bitmap(70,   20,   PixelFormat.Format24bppRgb);  
g   =   Graphics.FromImage(validateimage);  
g.DrawString(VNum,   new   Font( "Verdana ",12),new   SolidBrush(Color.White),new   PointF(8,0));  
g.FillRectangle(new   LinearGradientBrush(new   Point(0,0),   new   Point(110,20),   Color.FromArgb(0,0,0,0),Color.FromArgb(255,255,60,40)),0,0,120,30);  
g.Save();  
MemoryStream   ms=new   MemoryStream();  
validateimage.Save(ms,System.Drawing.Imaging.ImageFormat.Gif);  
Response.ClearContent();  
Response.ContentType= "image/gif ";  
Response.BinaryWrite(ms.ToArray());  
Session[ "VNum "]   =   VNum;
Response.End();  
}  

///用户登陆页面

if   (Session[ "VNum "]   !=   null)
{
if(this.txtValidate.Text   !=   Session[ "VNum "].ToString())
{
this.Label1.Visible   =   true;
this.Label1.Text   =   "验证码错误! ";
}
else
{
int   val   =   new   BLL.RegUsers().CheckLogingUser(this.userName.Text,System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(this.userPwd.Text, "md5 "));
if(val   >   0   )
{
Session[ "username "]   =   this.userName.Text;
Response.Redirect( "office/index.aspx ");
}
else
{
Response.Write( " <script> alert( '提示:帐号或密码错误! ');self.location.href= 'login.aspx ' </script> ");
}
}
}

弄得没办法了。只能先在最开始加一个if   (Session[ "VNum "]   !=   null)可第一次登陆正确总会跳回登陆页面。要登陆两次才行。。。

好心的帅哥帅姐们。帮帮小弟。。~~~~~~~~~~~

------解决方案--------------------
Response.BinaryWrite(ms.ToArray()); 它的原因