how to use Captcha in asp.net Second post

 Use of Captcha  in asp.net Second post

In Asp.net Programming Basically the user is shown an image with some characters and user has to fill the same character in a textbox provided, then the content of the image is matched with the textbox and if it matches it ensures that it’s a valid submission and the form gets submitted.

When we want to protect our web page Access by the automatic software or utility program. then we want to insured  that that is human which access my page.

<For view the full code Asp.net go to the following link and if you need the download this also.

code for .aspx page:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register Assembly="MSCaptcha" Namespace="MSCaptcha" TagPrefix="cc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript" language="javascript">
        function Check() {
            if (document.getElementById("<%=TxtName.ClientID%>").value.trim() == "") {
                alert("Please enter  Name.");
                document.getElementById('<%=TxtName.ClientID%>').focus();
                return false;
            }
           
            }


            else if (document.getElementById("<%=Txtweb.ClientID%>").value.trim() == "") {
                alert("Please enter web Address.");
                document.getElementById('<%=Txtweb.ClientID%>').focus();
                return false;
            }


          
            return confirm("Are you sure you want to save?");
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <table  style="width: 100%">
        <tr>
            <td align="center" colspan="2">
                &nbsp;</td>
        </tr>
      <tr>
            <td align="right" style="width:50%">
                Name :</td>
            <td align="left">
                <asp:TextBox ID="TxtName" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td align="right">
                Web Address :</td>
            <td align="left">
                <asp:TextBox ID="Txtweb" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td align="right">
                &nbsp;</td>
            <td align="left">
                <div class="row">
            <label>
            </label>
            <cc1:captchacontrol ID="ccJoin" runat="server" CaptchaBackgroundNoise="none" CaptchaLength="5"
                CaptchaHeight="40" CaptchaWidth="150" CaptchaLineNoise="None" CaptchaMinTimeout="5"
                CaptchaMaxTimeout="140" FontColor="Red" Width="155px" />
            <asp:TextBox ID="txtCaptcha" runat="server" placeholder="Type above text."></asp:TextBox>
        </div>
        </td>
        </tr>
        <tr>
            <td align="right">
                &nbsp;</td>
            <td align="left">
              <asp:Button ID="btnSave" runat="server" class="GWS_btn even" Style="margin-right: 10px;"
                OnClientClick="javascript:return Check();" Text="Submit" OnClick="btnSave_Click" />
            <asp:Label ID="lblMsg" runat="server" Visible="False" ForeColor="Red" Font-Bold="true"
                Font-Size="12px"></asp:Label>
            </td>
        </tr>
    </table>
    </div>
    </form>
</body>
</html>

Code for .aspx.cs Page :


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        ccJoin.ValidateCaptcha(txtCaptcha.Text);
        if (!ccJoin.UserValidated)
        {
            lblMsg.Visible = true;
            lblMsg.Text = "Invalid Text. Try Again!";
            return; //Inform user that his input was wrong ...
        }
    }
}


Code for Web config file .


<?xml version="1.0"?>
<configuration>
<system.web>
    <httpHandlers>

      <add verb="GET" path="CaptchaImage.axd" type="MSCaptcha.CaptchaImageHandler, MSCaptcha  "/>
      <add path="ChartImg.axd" verb="GET,HEAD,POST" type="System.Web.UI.DataVisualization.Charting.ChartHttpHandler, System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
   validate="false" />
    </httpHandlers>
</system.web>
  <system.webServer>
    <validation validateIntegratedModeConfiguration="false"/>
    <handlers>
      
      <add name="MSCaptcha" verb="GET" path="CaptchaImage.axd" type="MSCaptcha.CaptchaImageHandler, MSCaptcha  "/>
      <remove name="ChartImageHandler" />
      <add name="CrystalImageHandler.aspx_GET" verb="GET" path="CrystalImageHandler.aspx"
       type="CrystalDecisions.Web.CrystalImageHandler, CrystalDecisions.Web, Version=13.0.2000.0, Culture=neutral, PublicKeyToken=692fbea5521e1304"
       preCondition="integratedMode" />
      

    </handlers>
    <security>
      <requestFiltering>
        <requestLimits maxAllowedContentLength="4294967295"/>
      </requestFiltering>
    </security>
  </system.webServer>
  
</configuration>

For view the full code go to the following link and if you need the download this also.







Reference :

Popular posts from this blog