简体   繁体   中英

asp.net captcha validation using session

I`ve implemented the CAPTCHA in asp.net and it worked good before validation , but when I am to validate it , the program ignores any input text and behave as it is "incorrect" , can you tell me whats wrong with it or what can I do please? here is the code:

public partial class WebForm1 : System.Web.UI.Page
{
    //string strResult;
    protected string Captcha()
    {
        string alphabt = "QWERTYUIOPASDFGHJKLZXCVBNM0123456789";
        Random rand = new Random();

        int num1 = rand.Next(1, 36);
        int num2 = rand.Next(1, 36);
        int num3 = rand.Next(1, 36);
        int num4 = rand.Next(1, 36);
        int num5 = rand.Next(1, 36);
        int num6 = rand.Next(1, 36);

        string subStr1 = alphabt.Substring(num1, 1);
        string subStr2 = alphabt.Substring(num2, 1);
        string subStr3 = alphabt.Substring(num3, 1);
        string subStr4 = alphabt.Substring(num4, 1);
        string subStr5 = alphabt.Substring(num5, 1);
        string subStr6 = alphabt.Substring(num6, 1);

        string result = subStr1 + subStr2 + subStr3 + subStr4 + subStr5 + subStr6;
        Session["res"] = result;

        Bitmap bmpImage = new Bitmap(Server.MapPath("~/Image/bg1.jpg"));
        Graphics objGraphic = Graphics.FromImage(bmpImage);
        objGraphic.DrawString(result, new Font("arial", 12, FontStyle.Strikeout), SystemBrushes.WindowText, new PointF());
        string time = DateTime.Now.Ticks.ToString();//due to browser image cashing      
                                                    //bmpImage.Save(Server.MapPath("~/Image/securityImage.jpg"));
                                                    //return "~/Image/securityImage.jpg";
        bmpImage.Save(Server.MapPath("~/Image/securityImage" + time + ".jpg"));

        return "~/Image/securityImage" + time + ".jpg";
    }

    protected bool CaptchaValidate()
    {
        if (Session["res"].ToString() == TextBox1.Text)
            return true;
        return false;
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            Image1.ImageUrl = Captcha();
    }

    protected void btn1_Click(object sender, EventArgs e) //using ajax to refresh the picture
    {
        Image1.ImageUrl = Captcha();
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        if (CaptchaValidate() == false)
            Label1.Text = "Incorrect ";
        else
            Label1.Text = "correct";
    }
}

thanks in advance...

I just restarted my machine and , that was it ! bye the way , code above is correct .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM