对你一定很简单,验证码 session问题
生成图形页:
public class imgvil : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
string tmp = RndNum(4);
// HttpCookie a = new HttpCookie( "ImageV ",tmp);
// Response.Cookies.Add(a);
Session [ "mysession "]=tmp;
this.ValidateCode(tmp);
// 在此处放置用户代码以初始化页面
}
private void ValidateCode(string VNum)
{
Bitmap Img=null;
Graphics g=null;
MemoryStream ms=null;
int gheight=VNum.Length*12;
Img=new Bitmap(gheight,25);
g=Graphics.FromImage(Img);
//背景颜色
g.Clear(Color.White);
//文字字体
Font f=new Font( "Arial Black ",10);
//文字颜色
SolidBrush s=new SolidBrush(Color.Black);
g.DrawString(VNum,f,s,3,3);
ms=new MemoryStream();
Img.Save(ms,ImageFormat.Jpeg);
Response.ClearContent();
Response.ContentType= "image/Jpeg ";
Response.BinaryWrite(ms.ToArray());
g.Dispose();
Img.Dispose();
Response.End();
}
private string RndNum(int VcodeNum)
{
string Vchar= "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[] VcArray=Vchar.Split(new Char [] { ', '});
string VNum= " ";
int temp=-1;
Random rand=new Random();
for(int i=1;i <VcodeNum+1;i++)
{
if(temp!=-1)
{
rand=new Random(i*temp*unchecked((int)DateTime.Now.Ticks));
}
int t=rand.Next(35);
if(temp!=-1&&temp==t)
{
return RndNum(VcodeNum);
}
temp=t;
VNum+=VcArray[t];
}
return VNum;
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
--------------------------------
调用页面:
private void Page_Load(object sender, System.EventArgs e)
{
ImageButton1.ImageUrl = "imgvil.aspx ";
string mystr= " ";
//mystr=Session [ "mysession "].ToString ();
Session.Abandon ();
ViewState [ "abc "]=mystr;
// 在此处放置用户代码以初始化页面
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用