Tuesday, January 26, 2010

Capta code generator

 In .aspx page

<tr>
 <td align="left" class="normaltext" height="25" style="width: 202px" valign="middle">
  <strong>Enter Security Code</strong></td>
                                      <td align="left" class="normaltext" height="25" valign="middle">
                                         <strong>:</strong>                                      </td>
                                      <td align="left" class="normaltext" colspan="2" height="25" valign="middle">
                                      <img src="JpegImage.aspx"><br>
      <p>
        <strong>Enter the code shown above:</strong><br>
        <asp:TextBox CssClass="button_form" id="CodeNumberTextBox" runat="server"></asp:TextBox><asp:RequiredFieldValidator ID="RequiredFieldValidator17"
                                                                                                                                        runat="server" ErrorMessage="Enter Confirmation Code" ControlToValidate="CodeNumberTextBox"
                                                                                                                                        ForeColor="" CssClass="blue_content" ValidationGroup="signupvalid" Display="none"
                                                                                                                                        SetFocusOnError="true">*</asp:RequiredFieldValidator>
        <br>
      </p>
      <p>
        <em class="notice">
          (Note: If you cannot read the numbers in the above<br>
          image, reload the page to generate a new one.)</em>
      </p>
      <p><asp:Label id="MessageLabel" runat="server"></asp:Label></p>
                                      </td>
                                  </tr>

In .aspx.cs page


  if (!this.IsPostBack)

            // Create a random code and store it in the Session object.
            this.Session["CaptchaImageText"] = GenerateRandomCode();

        else
        {
            // On a postback, check the user input.
            if (this.CodeNumberTextBox.Text == this.Session["CaptchaImageText"].ToString())
            {
                // Display an informational message.
                this.MessageLabel.CssClass = "info";
                this.MessageLabel.Text = "Correct!";
            }
            else
            {
                // Display an error message.
                this.MessageLabel.CssClass = "error";
                this.MessageLabel.Text = "ERROR: Incorrect, try again.";

                // Clear the input and create a new random code.
                this.CodeNumberTextBox.Text = "";
                this.Session["CaptchaImageText"] = GenerateRandomCode();
            }
        }

.jpegImage.aspx

using System;
using System.Data;
using System.Configuration;
using System.Collections;
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;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Drawing.Text;

public partial class JpegImage : System.Web.UI.Page
{
    private void Page_Load(object sender, System.EventArgs e)
    {
        // Create a CAPTCHA image using the text stored in the Session object.
        CaptchaImage ci = new CaptchaImage(
          this.Session["CaptchaImageText"].ToString(),
          200, 50, "Century Schoolbook");

        // 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();
    }

}
                               

Action Method in Mvc

In this section, you will learn about the action method of controller class. All the public methods of a Controller class are called Act...