应该是验证码和登录页没有关联起来。
你应该先访问登陆页,带上CookieContainer,得到cookie后,再访问你验证码页面(带上得到的cookie),这样得到的验证码图片才是正确的。

解决方案 »

  1.   

    用fiddler调试下,看看你的提交和浏览器的有什么不同。
      

  2.   

    C#带验证码登陆示例
    <asp:TextBox ID="txtUid" runat="server" Font-Size="9pt" Width="120px" 
              BackColor="White"></asp:TextBox>//登陆用户名
    <asp:TextBox ID="txtPwd" runat="server" Font-Size="9pt" TextMode="Password"
              Width="120px" style="margin-left: 0px" BackColor="White">
           </asp:TextBox>//登陆密码
    <asp:TextBox ID="txtVali" runat="server" Font-Size="9pt" Width="60px" 
              BackColor="White"></asp:TextBox>//验证码输入框
    <img id="Img1" align="left" alt="看不清,请点击我!" onclick="this.src=this.src+'?'" 
              src="../youyu/CheckCode.aspx" style="width: 49px; height: 22px" />//验证码显示
    <asp:ImageButton ID="ImageManage" runat="server" ImageUrl="~/image/b1.jpg" 
                      OnClick="btnLoad_Click"/>//登陆事件
    //登陆事件处理验证
    protected void btnLoad_Click(object sender, ImageClickEventArgs e)
        {
            HttpCookie cookie = Request.Cookies["CheckCode"];
            if (String.Compare(cookie.Value, txtVali.Text, true) != 0)
            {            Response.Write("<script lanuage=javascript>alert('验证码错误');location='javascript:history.go(-1)'</script>");
            }        else
            {
               DataSet ds = DB.reDs("select * from tb_HuenLian where UserName='" + txtUid.Text.Trim() + "' and PassWord='" + txtPwd.Text.Trim() + "'");
                int i = this.checkLogin(txtUid.Text, txtPwd.Text);
                if (i > 0)
                {
                    Session["id"] = ds.Tables[0].Rows[0][0].ToString();
                    Session["UserName"] = this.txtUid.Text;
                    Session["PassWord"] = this.txtPwd.Text;
                    Page.Response.Redirect("Yonghu.aspx");
                }
                else
                {
                    Response.Write("<script lanuage=javascript>alert('用户名称或密码错误!');location='javascript:history.go(-1)'</script>");
                }
            }
        }
        public int checkLogin(string loginName, string loginPwd)
        {
            SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["conn"]);
            SqlCommand myCommand = new SqlCommand("select count(*) from tb_HuenLian where UserName=@loginName and PassWord=@loginPwd", con);
            myCommand.Parameters.Add(new SqlParameter("@loginName", SqlDbType.NVarChar, 20));
            myCommand.Parameters["@loginName"].Value = loginName;
            myCommand.Parameters.Add(new SqlParameter("@loginPwd", SqlDbType.NVarChar, 50));
            myCommand.Parameters["@loginPwd"].Value = loginPwd;
            myCommand.Connection.Open();
            int i = (int)myCommand.ExecuteScalar();
            myCommand.Connection.Close();
            return i;
        }