Tuesday, January 26, 2010

Capta code generator

 In .aspx page

 <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>
        <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"
        <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><asp:Label id="MessageLabel" runat="server"></asp:Label></p>

In .aspx.cs page

  if (!this.IsPostBack)

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

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


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(
          200, 50, "Century Schoolbook");

        // Change the response headers to output a JPEG image.
        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.


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...