首页 新闻 会员 周边 捐助

验证码不起作用

0
悬赏园豆:20 [已关闭问题]

protected void Button1_Click(object sender, EventArgs e)
    {
        string connectionstring = "Data Source=CE5BC36663C4490\\SQLEXPRESS;Initial Catalog=Shop online;Integrated Security=True";
        SqlConnection conn = new SqlConnection(connectionstring);
        string str = "select * from Customer where Name='"+TextBox1 .Text +"' and Password='"+TextBox2 .Text +"'";
        SqlCommand cmd = new SqlCommand(str,conn );
        conn.Open();
        SqlDataReader dr = cmd.ExecuteReader();
        if(dr.Read ())
        {
            if (String.Compare(Session["CheckCode"].ToString(), TextBox3.Text, true)!= 0)
            {
                Response.Write("ok");
            }
            else
            {
                Response.Write("验证码不正确!");
            }
        }
    }

验证码.aspx.cs:

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        this.CreateCheckCodeImage(GenerateCheckCode());
    }

 


    private string GenerateCheckCode()
    {
        int number;
        char code;
        string checkCode = String.Empty;

        System.Random random = new Random();

        for (int i = 0; i < 6; i++)
        {
            number = random.Next();

            if (number % 2 == 0)
                code = (char)('0' + (char)(number % 10));
            else
                code = (char)('A' + (char)(number % 26));

            checkCode += code.ToString();
        }
        Session["CheckCode"] = checkCode;

        //Response.Cookies.Add(new HttpCookie("CheckCode", checkCode));

        return checkCode;
    }

    private void CreateCheckCodeImage(string checkCode)
    {
        if (checkCode == null || checkCode.Trim() == String.Empty)
            return;

        System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 22);
        Graphics g = Graphics.FromImage(image);

        try
        {
            //生成随机生成器
            Random random = new Random();

            //清空图片背景色
            g.Clear(Color.White);

            //画图片的背景噪音线
            for (int i = 0; i < 25; i++)
            {
                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 System.Drawing.Font("Arial", 12, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic));
            System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true);
            g.DrawString(checkCode, 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()));
            }

            //画图片的边框线
            g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);

            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
            Response.ClearContent();
            Response.ContentType = "image/Gif";
            Response.BinaryWrite(ms.ToArray());
        }
        finally
        {
            g.Dispose();
            image.Dispose();
        }
    }


}

验证码形同虚设,只要密码正确。。。

就可以登录。。。汗!

zlq的主页 zlq | 初学一级 | 园豆:10
提问于:2008-11-06 15:34
< >
分享
其他回答(1)
0

当然了,你的代码逻辑有问题!

将你的这一段代码:

 if(dr.Read ())
  {
  if (String.Compare(Session["CheckCode"].ToString(), TextBox3.Text, true)!= 0)
  {
  Response.Write("ok");
  }
  else
  {
  Response.Write("验证码不正确!");

 return;
  }
  }

另外呢,建议楼主先判断验证码是否正确,正确才连接数据库验证用户名和密码,否则就不用连接数据库了,这样节省一点资源。

上不了岸的鱼 | 园豆:4613 (老鸟四级) | 2008-11-06 16:33
0

。。。。 晕倒。 你生成验证码的时候是在Page_Load里面。 所以在你点击Button回发处理的时候,有执行了一次,那么验证码其实已经换掉了。 当然不正确啦。

要么在Page_Load里加个 if(!IsPostBack), 要么把生成验证码图片的逻辑提取出来作为一个文件,然后:<img src="CodeImage.aspx" />

Hunts.C | 园豆:266 (菜鸟二级) | 2008-11-07 14:39
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册