Popular Posts

Sunday, 20 July 2014

simple captcha in asp.net

SIMPLE OWNED CAPTCHA TO FOREPLAY BOTS...........



Here is the code for adding a simple captcha in your aspx code rather than using any third party.
use ur imagination and create your own.


THE ASPX PAGE:


<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeFile="Default.aspx.cs" Inherits="_Default" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
   
    <p>
        CAPTCHA :</p>
    <p>
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> + <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label> =
        <asp:TextBox ID="TextBox1" runat="server" Width="40px"></asp:TextBox>
        <br />
        <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
    </p>
   
</asp:Content>




Here we have added two lables and one text box to display the number (random ) and to get the result from the user respectively..


THE CODE BEHIND 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)
    {
        if (!IsPostBack)
        {
            Random _r = new Random();
            int n = _r.Next(10);
            Label1.Text = "0";
            Label2.Text = n.ToString();
        }

    }
    protected void Button1_Click(object sender, EventArgs e)
    {

        int result = Convert.ToInt32(TextBox1.Text            );
        int cal=Convert.ToInt32(Label1.Text)+Convert.ToInt32(Label2.Text);
        if (result.Equals(cal))
        {
            ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "errmsg", "alert('CORRECT CAPTCHA LOGIN SUCCESSFULL!!')", true);

        }
        else
        {

            ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "errmsg", "alert('INCORRECT TRY AGAIN')", true);

        }

    }
}


AND ITS DONE ..





No comments :

Post a Comment