Captcha Code


Hello readers. I have written a code to generate Captcha Code. I have created a DLL file for this, all u have 
to do is - just add reference of the DLL and use to generate captcha code.

1. Add Reference of the DLL  that is added to bin folder of your application.

2. Now add aspx page and add an Image control.

<asp:Image ID="Image1" runat="server" />
<asp:TextBox ID="txtCaptcha" runat="server"></asp:TextBox>
        <asp:Button ID="btnValidate" runat="server" Text="Validate" OnClick="btnValidate_Click" />
        <br />
        <br />
        <asp:Label ID="lblMessage" runat="server"></asp:Label></div>

 3. Now write in the .cs page

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using CaptchaBL;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        CaptchaCode cap = new CaptchaCode();
        cap.CodeType = 4;
        cap.DeletionTime = 1;
        cap.FontSize = 26;
        cap.BackgroundColor = "#90bbc8";
        cap.ForegroundColor = "#000000";
        cap.GetCaptchata();      
        Image1.ImageUrl = cap.ImageName;
        Session["captcha"] = cap.CaptchaText.ToLower();
    }

protected void btnValidate_Click(object sender, EventArgs e)
    {
        if (txtCaptcha.Text.CompareTo(Session["captcha"].ToString()) == 0)
        {
            lblMessage.Text = "Valid Code. Good Work...!";
        }
        else
        {
            GenrateCaptcha();
            lblMessage.Text = "Invalid Code..! Please try again.";
        }
    }
}

Here above in the code all the parameters are optional:-

CodeType limit is 0-4
Default value of CodeType = 0 - this generates alphanumeric captcha.
                                                        1 - generates Numbers only.
                                                        2 -Capital Characters
                                                        3-Small Characters
                                                        4- Small + Capital Characters
DeletionTime:
Deletiontime property deletes the code image file automatically after the time specified in minutes.
Here cap.DeletionTime = 1 means delete the images that are older than 1 minutes from the current code image file. Default value is 3 minutes.
FontSize:
Default value id 35px. You can change accordingly.  Height and width set automatically according to font size.

Foreground and Background color:
Default Foreground color is Black and Background is Gray.

cap.GetCaptcha()
Method genetares captcha image with code.
cap.CaptchaText
Returns the captcha code in text that is saved in the session variable to validate.

4. To validate captcha add captca to session variable using cap.CaptchaText.

5. Also you can use own background image for captcha background. Use cap.ImagePath = "path to image".
e.g., cap.ImagePath = "~/images/customback.jpg";

Download Code DLL included in Bin folder:-

https://docs.google.com/open?id=0B2iokvd3EpXAZDg4OWY2NzItN2Q2YS00YThkLThkMjItNTgzZGQzMTg0NzFk 

Comments

  1. Nice...It's so much usefully..
    Thanks For Share it.

    ReplyDelete
  2. heyyyy we can also generate code in alphanumerical form..for this change in dll method..for this u should provide parameter..

    ReplyDelete
  3. @ swati said...

    use cap.CodeType = 0; // for alphanumeric

    ReplyDelete

Post a Comment

Popular posts from this blog

Get Query String Values With Javascript

Change Css Class of li in ul onclick

Find missing sequence number. Get the numbers where sequence is missing.