SIGN UP MEMBER LOGIN:    
ARTICLE

Capchas in ASP.Net Web Form

Posted by Harshit Vyas Articles | ASP.NET Programming September 29, 2010
The capchas are really very good thing to prevent bots from autofilling and securing our site.
Reader Level:
Download Files:
 

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.

Login to add your contents and source code to this article
share this article :
post comment
 

thanks for the encouragement

Posted by Harshit Vyas Sep 30, 2010

Good One Keep up the good work.

Posted by Raj Kumar Sep 30, 2010
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Nevron Gauge for SharePoint
Become a Sponsor