How to Generate Captcha in Asp.Net


Description:-

CAPTCHA stands for "Completely Automated Public Turing test to tell Computers and Humans Apart". Since the dawn of the Internet, people have tried to abuse websites for both sport and profit. As the abuse became profitable, the scale of abuse grew using automated software. CAPTCHA "more secure" is to obfuscate the letters displayed with increasing severity.

Note: CAPTCHA implementations can be found on more than 3.5 million sites globally, and human beings solve CAPTCHA implementations more than 300 million times a day.

Step 1: Create Web page and Design for captcha like below.

<div style="margin-left: 300px; margin-top: 60px; width: 255px; text-align: center;
   text-decoration: underline; background-color: Yellow; text-transform: uppercase;">
   <h1 style="color: #FF0000">
      Captcha
   </h1>
</div>
<div style="border-style: groove; margin-left: 300px; margin-top: 60px; width: 257px;
   color: #008080; background-color: #C0C0C0;">
   <table border="0" cellpadding="3" cellspacing="0">
      <tr>
         <td colspan="3">
            Enter Text
            <asp:TextBox ID="txtCaptcha" runat="server"></asp:TextBox>
         </td>
      </tr>
      <tr>
         <td colspan="2">
            <cc1:CaptchaControl ID="Captcha1" runat="server" CaptchaBackgroundNoise="Low" CaptchaLength="5"
               CaptchaHeight="60" CaptchaWidth="200" CaptchaMinTimeout="5" CaptchaMaxTimeout="240"
               FontColor="#FF0040" NoiseColor="#F4FA58" CacheStrategy="HttpRuntime" />
         </td>
         <td>
            <asp:ImageButton ID="ImageButton1" ImageUrl="~/refresh.png" runat="server" CausesValidation="false" />
         </td>
      </tr>
      <tr>
         <td>
            <asp:CustomValidator ID="CustomValidator1" ErrorMessage="Invalid. Please try again."
               OnServerValidate="ValidateCaptcha" runat="server" />
         </td>
         <td align="right">
            <asp:Button ID="btnSubmit" runat="server" Text="Submit" />
         </td>
         <td>
         </td>
      </tr>
   </table>
</div>

Step 2: Now Code for Create Captcha in code behind to Generate Captcha. On Validation we Will Generate Captcha and Call to Server.

Protected void ValidateCaptcha(object sender, ServerValidateEventArgs e)
{
    Captcha1.ValidateCaptcha(txtCaptcha.Text.Trim());
    e.IsValid = Captcha1.UserValidated;
    if (e.IsValid)
    {
        ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Valid Captcha!');", true);
    }
}

Step 3: For That We Will Use Namespaces.

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;

Step 4: Don’t Forget to Check or Create handlers in web.config File.

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

Step 5: Now Open your Webpage and Check for Create Captcha is Generated or What ?


Step 6: Now Fill Textbox from what you see in Captcha Image. And Click Submit button for Validation Captcha Image.

Related Posts

Previous
Next Post »

1 comments:

comments
Anonymous
November 8, 2017 at 3:08:00 AM GMT+5:30 delete

Heya i ɑm fߋr the fіrst time here. I came acroѕѕ thіs board and
I find Ιt truly uѕeful & іt helped me out much. I hope to
give something bɑck ɑnd help others like you helped me.

Reply
avatar

Thanks for comments.....