protected void Page_Load(object sender, EventArgs e)
    {
        this.Image1.ImageUrl = "ValidateCode2.aspx";//获取验证码
   
    }
    protected void btnLogin_Click(object sender, EventArgs e)//登陆按钮
    { 
        if (txtAdminName.Text.Trim() == "" || txtAdminPwd.Text.Trim() == "")
        {
            Response.Write(CC.MessageBox("登录名和密码不能为空!", "Login.aspx"));
                    }
        else
        {
            //判断用户输入的验证码是否正确
//此处出错
            if (Session["CheckCode"].ToString().Equals(txtAdminCode.Text.ToString().ToUpper()))

            {
                            
                string UserName = txtAdminName.Text;
                string UserPwd = txtAdminPwd.Text;
                string selsql = "select * from tbUser where (Name='" + UserName + "')and(passWord='" + UserPwd + "')";
                SqlDataReader sdr = operateData.getRow(selsql);                if (operateData.getCount(selsql) > 0)
                {
                    //该用户为合法用户,跳转到后台首页(AdminIndex.aspx)中
                    Session["admin"] = txtAdminName.Text.Trim();//用户名
                    sdr.Read();
                    Response.Redirect("../NewsManage/AdminIndex.aspx");
                }
                else
                {
                    //该用户不是合法用户,调用CommonClass类中的MassageBox方法,弹出提示框
                    Response.Write(CC.MessageBox("您输入的用户名或密码错误,请重新输入!", "Login.aspx"));                }
            }
            else
            {
                Response.Write(CC.MessageBox("验证码输入有误,请重新输入!", "Login.aspx"));            }
        }
    }

解决方案 »

  1.   

    Session["CheckCode"].ToString()...
    你不判断Session["CheckCode"]是否为空就ToString...出错正常了
    第一次打开.都没存过checkcode.能不空?
      

  2.   

    首次登陆的确是空,是我程序的漏洞,但我是在ValidateCode2.aspx中设置的Session["CheckCode"]
    为什么进入登陆页面时Session["CheckCode"]为空呢?
    ValidateCode2.aspx的代码
     protected void Page_Load(object sender, EventArgs e)
        {
            //string checkCode = GetRandom();
            //Session["CheckCode"] = checkCode;
            DrawRandom();
        }
        private string GetRandom()
        {
            int number;
            char charCode;
            string strCode = string.Empty;
            System.Random random = new System.Random();        for (int i = 1; i <= 5; i++)
            {
                number = random.Next();
                if (number % 2 == 0)
                {
                    charCode = (char)('0' + (char)(number % 10));
                }
                else
                {
                    charCode = (char)('A' + (char)(number % 26));
                }
                strCode += charCode.ToString();
            }
            // System.Web.HttpContext.Current.Response.Cookies 
            HttpContext.Current.Response.Cookies.Add(new HttpCookie("codebycookies", strCode));
            return strCode;
        }    public void DrawRandom()
        {        string strCode = GetRandom();
            Session["CheckCode"] = strCode;
                  Bitmap image = new Bitmap(100, 30);
            Graphics huatu = Graphics.FromImage(image);
            try
            {
                //生成随机生成器 
                Random random = new Random();
                //清空图片背景色 
                huatu.Clear(Color.White);            //画图片的背景噪音线 
                int x1, x2, y1, y2;
                for (int i = 0; i < 10; i++)
                {
                    x1 = random.Next(image.Width);
                    x2 = random.Next(image.Width);
                    y1 = random.Next(image.Height);
                    y2 = random.Next(image.Height);
                    huatu.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
                }            Font font = new Font("Arial", 20, (FontStyle.Bold | System.Drawing.FontStyle.Italic));
                LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true);
                huatu.DrawString(strCode, 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()));
                }
                //画图片的边框线 
                huatu.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width, image.Height);
                System.IO.MemoryStream ms = new System.IO.MemoryStream();
                image.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);            HttpContext.Current.Response.ClearContent();
                HttpContext.Current.Response.BinaryWrite(ms.ToArray());
            }
            finally
            {
                huatu.Dispose();
                image.Dispose();
            }
      

  3.   

    并且这个问题在google chrome 中不存在
      

  4.   


    protected void Page_Load(object sender, EventArgs e)
      {
                if (!IsPostBack)
                {
     //代码放这里            }
      }
      

  5.   

    是缓存的问题,IE8对于图片加载是尽量使用本地缓存。完全不发送http请求。
    所以通常的解决方案是用脚本改变图片的src来加载验证码,加载时在链接后面加上随机的query。不过这还得看你这个图片响应时的http头,如果http头里面约束了不使用private缓存。ie8也不会缓存图片。
      

  6.   

    可以解决的,在页面生成验证码的时候都应该保存在session内,这样就不为空了,你在找个生成验证码的方法就行了
      

  7.   

    你的验证码的是本页面产生的,并非异步请求,要在CS文件里,设置该页不缓存
    private void SetPageNoCache()
            {
                Response.Buffer = true;
                Response.ExpiresAbsolute = System.DateTime.Now.AddSeconds(-1);
                Response.Expires = 0;
                Response.CacheControl = "no-cache";
                Response.AppendHeader("Pragma", "No-Cache");
            }
    在PAGELOAD里调用一下!
    判断session是否为空,在PAGELODA中用
    if(!ispostback){}判断
    如果session不存在,就会出现 你现在的这种异常!System.NullReferenceException: 未将对象引用设置到对象的实例
      

  8.   


    是写成这样吗?this.Image1.ImageUrl = "ValidateCode2.aspx?id=" + DateTime.Now.ToFileTime().ToString(); 可是这样还是缓存图片啊,还是需要刷新才能找到session
    http头里面约束不使用private缓存是什么意思?
      

  9.   


    我生成验证码的时候就已经把验证码保存在session内了
      

  10.   


    是不是写成这样?
     this.Image1.ImageUrl = "ValidateCode2.aspx?id=" + DateTime.Now.ToFileTime().ToString();
    但还是不行
      

  11.   

    这个说的好像很有道理。建议大家看到最后。asp.net chrome浏览器无法使用session的原因和解决办法  
    http://www.suchso.com/projecteactual/aspnet-chrome-cookie-session-id.html