Capchas in ASP.Net Web Form


The capchas are really very good thing to prevent bots from autofilling and securing our site.

We can generate the capchas by our code but we can get some ready made capchas so there is no need to generate them and wasting time especially when they are very good.

Here I am going to show two types of capchas and the screen shot of those capchas are as below:

Screen shot of the capchas:-

1.gif

You can see that both capchas are very good. The second one is very famous as it is provided by the google and known as recapchas.

First I am going to explain about first capcha which is known as mscapcha.

First you need to download the dll file for ms capcha which can found from 


After downloading just put that dll in your bin file folder. If you have downloaded my example than you can directly get that from my example's bin folder.

After putting in bin folder you need to add the reference of this dll in your project and modify your web.config file by putting this line

<add verb="GET" path="CaptchaImage.axd" type="MSCaptcha.CaptchaImageHandler, MSCaptcha"/>

Under  <httphandlers> section

After that it will look like:

<httpHandlers>
      <add verb="GET"  path="CaptchaImage.axd"  type="MSCaptcha.CaptchaImageHandler, MSCaptcha"/>
</httpHandlers>

Now just add one line on your page where you are going to use this capcha:

<%@  Register Assembly="MSCaptcha"  Namespace="MSCaptcha" TagPrefix="cc1" %>

And you are ready to use this capcha; add the capcha anywhere such as:

<cc1:CaptchaControl ID="ccJoin" runat="server" FontColor="Red" ForeColor="AliceBlue"  NoiseColor="Red" CaptchaBackgroundNoise="Extreme"  CaptchaLength="5" CaptchaHeight="40" CaptchaWidth="150" CaptchaLineNoise="None" CaptchaMinTimeout="5" CaptchaMaxTimeout="240"  />

It will give you new capcha every time when the page refresh occurs.

To validate this capcha, you simply need to check like:

protected void Button1_Click(object sender, EventArgs e)
{
    ccJoin.ValidateCaptcha(txt1.Text);
    if (!ccJoin.UserValidated)
    {
        Page.RegisterStartupScript("asd", "<script language='javascript'>alert('capchu khotu 6');</script>");
    }
    else
    {
        Page.RegisterStartupScript("asd", "<script language='javascript'>alert('capchu sachu 6');</script>");
    }
}

From your .cs page

Now for the recapcha provided by google.

First, in order to use recapcha you have to register yourself from here http://www.google.com/recaptcha/whyrecaptcha

After registration you will get two keys

PublicKey, PrivateKey which are very useful

You also need to download recapcha dll which can be found from here


After downloading that dll you have to add that as a reference. In the page on which you want to use it add this line:

<%@ Register TagPrefix="recaptcha" Namespace="Recaptcha" Assembly="Recaptcha" %>

After doing that you are ready to use recapcha,.
Just add this tag anywhere to use recapcha:

<recaptcha:RecaptchaControl
    ID="recaptcha"
    runat="server" BackColor="AliceBlue" ForeColor="Red"
    PublicKey="6LeUS70SAAAAAGVK9oomx8q1rJDg607uvLbH9Ni3"
    PrivateKey="6LeUS70SAAAAADbKYFeMws-jhgmQIAwfkfTRm70r"
/>

Also the validation is very simple just like this:

protected void Button1_Click(object sender, EventArgs e)
{
    if (Page.IsValid)
    {
        Page.RegisterStartupScript("asd", "<script labguage='javascript'>alert('success');</script>");
    }
    else
    {
        Page.RegisterStartupScript("asd", "<script labguage='javascript'>alert('un success');</script>");
    }
}

For more details download source code and run it to see the effects. There are some more tags to set noise size font types; try that out.

Thanks for reading.


Similar Articles