How to make math captcha in C#.net

In spare time, make captcha in math style using C# – ASP.Net. First thing first, create the database. Then show the captcha randomly. I know it’s unsecure this way. Better replace the captcha with image.

View:

  <asp:Label ID="capcay" runat="server" Text="Label"></asp:Label>
    <asp:HiddenField ID="hd_capcay" runat="server" />
    <asp:TextBox ID="a_capcay" runat="server"></asp:TextBox>
     <asp:Button ID="Button4" runat="server" onclick="Button4_Click" Text="Button" />
     <asp:Label ID="validd" runat="server" Text="Label"></asp:Label>
     <asp:Label ID="dump" runat="server" Text="Label" Visible="false"></asp:Label>

Controller

 private RetnoTest.Library.Master.Test msc = new Library.Master.Test();
        private DbDataReader MyRead;
        protected void Page_Load(object sender, EventArgs e)
        {
            dump.Text = hd_capcay.Value;
            retrieve_capcay();
        }
  protected void retrieve_capcay()
        {
            MyRead = msc.Capcay_browse();
            hd_capcay.Value = MyRead[0].ToString();
            capcay.Text = MyRead[1].ToString();
        }
        protected void jawab_capcay() {            
            MyRead = msc.jawab_capcay(dump.Text, a_capcay.Text);
            if (MyRead.HasRows)
            {
                    validd.Text = "benar!";
                    retrieve_capcay();
            }
            else {
                validd.Text = "salah. itung lagi";
                
            }
            
            a_capcay.Text = "";
        }
        protected void Button4_Click(object sender, EventArgs e)
        {
             jawab_capcay();            
        }

Model

   public DbDataReader Capcay_browse()
        {
            strSQL = "SELECT TOP 1 * FROM MC_Capcay order by newid()"; // menampilkan data di SQLServer secara acak
            return Helper.ExecuteReader(strSQL);
        }
        public DbDataReader jawab_capcay(string id, string jawab)
        {
            strSQL = "SELECT * FROM MC_Capcay WHERE id = '" + id + "' and jawaban = '"+ jawab +"'";
            return Helper.ExecuteReader(strSQL);
        }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s