Thursday, April 5, 2012

ASP.Net(Self Maid Custom Captcha)


//Step:01 Create A Page With this function On Load
protected void Page_Load(object sender, EventArgs e)
    {
        Random num1 = new Random();

        int numQ1;
        int numQ2;
        int numQ3;
        int numQ4;
        int numQ5;
        int numQ6;
        string QString;


        numQ1 = num1.Next(97, 122);
        numQ2 = num1.Next(97, 122);
        numQ3 = num1.Next(48, 57);
        numQ4 = num1.Next(65, 90);
        numQ5 = num1.Next(48, 57);
        numQ6 = num1.Next(65, 90);

        QString = Char.ConvertFromUtf32(numQ1) + "" + Char.ConvertFromUtf32(numQ2) + "" + Char.ConvertFromUtf32(numQ3) + "" + Char.ConvertFromUtf32(numQ4) + Char.ConvertFromUtf32(numQ5) + Char.ConvertFromUtf32(numQ6);
        Session["SessionName"] = QString;


        Bitmap bitmap = new Bitmap(200, 60);
        Graphics Grfx = Graphics.FromImage(bitmap);
        Font font = new Font("Times New Roman", 35, FontStyle.Bold, GraphicsUnit.Pixel);
        Rectangle Rect = new Rectangle(0, 0, 200, 60);

        Grfx.FillRectangle(Brushes.White, Rect);
        Grfx.DrawRectangle(Pens.White, Rect);
        Grfx.DrawString(QString, font, Brushes.Black, 10, 7);


        Pen blackPen = new Pen(Color.Red, 2);

        // Create points that define line.

        Random r = new Random();
        int p1 = r.Next(0, 60);
        int p2 = r.Next(10, 155);

        Point point1 = new Point(p1, 60);
        Point point2 = new Point(155, p2);

        Point point3 = new Point(60, p1);
        Point point4 = new Point(p2, 155);

        // Draw line to screen.
        Grfx.DrawLine(blackPen, point1, point2);
        Grfx.DrawLine(blackPen, point3, point4);
       
        Point[] pnt = new Point[3];
        pnt[0] = new Point(r.Next(0,70), r.Next(0,70));
        pnt[1] = new Point(r.Next(37,70), r.Next(0,36));
        pnt[2] = new Point(r.Next(0,36), r.Next(0,35));

        Grfx.DrawCurve(blackPen, pnt);

        Response.ContentType = "Image/jpeg";
        bitmap.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);


        bitmap.Dispose();
        Grfx.Dispose();
    }


//Step:02 Image Control To Show The Captcha
<asp:Label ID="lblCaptchaError" ForeColor="Red" Font-Italic="true" Font-Size="10" runat="server" Text=""></asp:Label>
<asp:Image ID="imageCaptcha" runat="server" ImageUrl="~/pageName.aspx" />
<asp:TextBox ID="txtCaptcha" runat="server"></asp:TextBox>
//ImageUrl= Page Name


//Step:03(a) Change Image New Image
<asp:LinkButton ID="lbtnNewCode" runat="server" OnClick="lbtnNewCode_Click">
Try a new code</asp:LinkButton>

//Step:03(b) Change Image New Image
protected void lbtnNewCode_Click(object sender, EventArgs e)
{
    imageCaptcha.ImageUrl = "pageName.aspx";
}

//Step:04(a) Captcha Validate
private bool ValidCaptcha()
    {
        if (Session["SessionName"] != null)
        {
            if (txtCaptcha.Text != Session["SessionName "].ToString())
            {
                lblCaptchaError.Text = "Invalid Captcha Text";
                return false;
            }
            else
            {
                lblCaptchaError.Text = "";
                return true;
            }
        }
        else
        {
            lblCaptchaError.Text = "Invalid Captcha Text";
            return false;
        }
    }


//Step:04(b) Button Click

protected void btn_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            lblError.Visible = false;
            if (txtCaptcha.Text != null)
            {
                if (ValidCaptcha())
                {
                    txtCaptcha.Text = string.Empty;
      
             //empty captcha text
                    Response.Redirect("~/YourPage.aspx");

                }
                else
                {
                    //empty captcha text
                    txtCaptcha.Text = string.Empty;
                }
            }
        }
        catch (Exception)
        {
            return;
        }


No comments:

Post a Comment