Click here to Skip to main content
16,016,750 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have used the code from [Code_Project] to implement captcha in my website.

The code seems to work fine, but the captcha appears on a different/blank page.
Also, the code below is used, but when the captcha is generated, the textfields en button aren't visible. Can anyone help me out here?
XML
<asp:Panel ID="P_LastStepOrder" runat="server" Visible="false">
    <div style="width: 400px; float:left; margin-left:15px">
        <fieldset style=" width:350px">
            <legend>Customer Contact Info</legend>
            <asp:Label ID="L_CustName" runat="server" Width="200px" Text="Customer name:"></asp:Label>
            <br />
            <asp:TextBox ID="TB_CustName" runat="server" Width="200px"></asp:TextBox>
            <br />
            <asp:Label ID="L_CustEmail" runat="server" Width="200px" Text="Customer email address:"></asp:Label>
            <br />
            <asp:TextBox ID="TB_CustEmail" runat="server" Width="200px"></asp:TextBox>
            <br />
            <br />
            <div>
                 <asp:Label ID="L_CaptchaCheck" runat="server" Font-Bold="True" ForeColor="Red" Text=""></asp:Label>
                 <br />
            </div>
            <asp:TextBox ID="TB_CaptchaImageCode" runat="server"></asp:TextBox>
            <br />
            <asp:Image ID="I_Captcha" runat="server" ImageUrl="~/Webpages/ShoppingCart.aspx"/>
            <br />
            <asp:ImageButton ID="IB_SubmitOrder" runat="server" CausesValidation="False" ImageUrl="~/Images/IB_SubmitOrder.jpg"
                            ToolTip="Submit order" Height="50px" Width="180px" OnClick="Email"/>
        </fieldset>
    </div>
</asp:Panel>


These labels etc are located in a Panel which I set visible as soon as a button is clicked, which also triggers the load_Captcha code. I've also noticed that the specified ImageUrl for image I_Captcha does not do anything at all.


Best regards,
Mati
Posted
Updated 26-May-14 3:48am
v4

Apperently I had to create a new aspx page and add the code on that page.

C#
using System;
using System.Collections.Generic;
using System.Drawing.Imaging;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace x.Webpages
{
    public partial class JpegImageCaptcha : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            // Create a random code and store it in the Session object.
            this.Session["CaptchaImageText"] = GenerateRandomCode();
            // Create a CAPTCHA image using the text stored in the Session object.
            RandomImage ci = new RandomImage(this.Session["CaptchaImageText"].ToString(), 300, 75);
            // Change the response headers to output a JPEG image.
            this.Response.Clear();
            this.Response.ContentType = "image/jpeg";
            // Write the image to the response stream in JPEG format.
            ci.Image.Save(this.Response.OutputStream, ImageFormat.Jpeg);
            // Dispose of the CAPTCHA image object.
            ci.Dispose();
        }

        // Function to generate random string with Random class.
        private string GenerateRandomCode()
        {
            Random r = new Random();
            string s = "";
            for (int j = 0; j < 5; j++)
            {
                int i = r.Next(3);
                int ch;
                switch (i)
                {
                    case 1:
                        ch = r.Next(0, 9);
                        s = s + ch.ToString();
                        break;
                    case 2:
                        ch = r.Next(65, 90);
                        s = s + Convert.ToChar(ch).ToString();
                        break;
                    case 3:
                        ch = r.Next(97, 122);
                        s = s + Convert.ToChar(ch).ToString();
                        break;
                    default:
                        ch = r.Next(97, 122);
                        s = s + Convert.ToChar(ch).ToString();
                        break;
                }
                r.NextDouble();
                r.Next(100, 1999);
            }
            return s;
        }
    }
}


Nothing was added in the ASP.NET part of the webpage.

KR
 
Share this answer
 
Don't post this under Quick Answers - if you got the code from an article, then there is a "new message" button at the bottom of that article, which causes an email to be sent to the author. They are then alerted that you wish to speak to them.
Posting this here relies on them "dropping by" and realising it is for them.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900