Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the principle and application of ASP.NET1.1 CAPTCHA?

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Today, I will talk to you about the principle and application of ASP.NET1.1 CAPTCHA, which may not be well understood by many people. in order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.

Implementation principle of ASP.NET1.1 CAPTCHA

CAPTCHA elements are generated by random functions, the numeric CAPTCHA elements are converted into characters and then concatenated into strings, and the CAPTCHA strings are written into Cookie for verification.

Through the method of dynamic bitmap drawing in the background, draw a bitmap of a specified size, and then draw the shading, CAPTCHA font, and frame in the blank bitmap.

ASP.NET1.1 CAPTCHA implementation code

(1) Login.aspx (front desk of login page)

< %@ Page language="c#" Codebehind="Login.aspx.cs" AutoEventWireup="false" Inherits="Validator.Login" %>

< !DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >

< HTML>

< HEAD>

< title>

Login

< /title>

< meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">

< meta name="CODE_LANGUAGE" Content="C#">

< meta name="vs_defaultClientScript" content="JavaScript">

< meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">

< /HEAD>

< body MS_POSITIONING="GridLayout">

< form id="Form1" method="post" runat="server">

< asp:button id="Button1" style="Z-INDEX: 101; LEFT: 128px; POSITION: absolute; TOP: 64px" runat="server" Width="96px" Text="提交">

< /asp:button>

< IMG src="CheckCode.aspx">

< asp:label id="lblMessage" style="Z-INDEX: 102; LEFT: 16px; POSITION: absolute; TOP: 128px" runat="server">

< /asp:label>

< asp:textbox id="txtCheckCode" style="Z-INDEX: 103; LEFT: 16px; POSITION: absolute; TOP: 64px" runat="server" Width="88px">

< /asp:textbox>

< /form>

< /body>

< /HTML>

(2) Login.aspx.cs (backend of login page)

Using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; namespace Validator {/ * /

< summary>

/ / A summary description of the Login. / / /

< /summary>

Public class Login: System.Web.UI.Page {protected System.Web.UI.WebControls.Button Button1; protected System.Web.UI.WebControls.Label lblMessage; protected System.Web.UI.WebControls.TextBox txtCheckCode Private void Page_Load (object sender System.EventArgs e) {/ / place user code here to initialize page} code generated by the Web forms designer # code generated by the region Web forms designer override protected void OnInit (EventArgs e) {/ CODEGEN: this call is required by the asp.net Web forms designer. / / InitializeComponent (); base.OnInit (e);} / * /

< summary>

/ / the designer supports the required methods-do not use the code editor to modify the contents of this method. / / /

< /summary>

Private void InitializeComponent () {this.Button1.Click + = new System.EventHandler (this.Button1_Click); this.Load + = new System.EventHandler (this.Page_Load) } # endregion private void Button1_Click (object sender, System.EventArgs e) {if (Request.Cookies ["CheckCode"] = = null) {lblMessage.Text = "your browser settings have been disabled Cookies, you must set the browser to allow the use of Cookies option before you can use this system." ; lblMessage.Visible = true; return;} if (String.Compare (Request.Cookies ["CheckCode"] .Value, txtCheckCode.Text, false)! = 0) / / case-sensitive {lblMessage.Text = "CAPTCHA error, please enter the correct CAPTCHA when the parameter is false." ; lblMessage.Visible = true; return;} else {lblMessage.Text = "verified"; lblMessage.Visible = true; return;}}

(3) CheckCode.aspx (foreground of verification page)

< %@ Page language="c#" Codebehind="CheckCode.aspx.cs" AutoEventWireup="false" Inherits="Validator.CheckCode" %>

< !DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >

< html>

< head>

< title>

CheckCode

< /title>

< meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">

< meta name="CODE_LANGUAGE" Content="C#">

< meta name=vs_defaultClientScript content="JavaScript">

< meta name=vs_targetSchema content="http://schemas.microsoft.com/intellisense/ie5">

< /head>

< body MS_POSITIONING="GridLayout">

< form id="Form1" method="post" runat="server">

< FONT face="宋体">

< /FONT>

< /form>

< /body>

< /html>

(4) CheckCode.aspx.cs (background of verification page)

Using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; namespace Validator {/ * /

< summary>

/ / A summary description of the CheckCode. / / /

< /summary>

Public class CheckCode: System.Web.UI.Page {private void Page_Load (object sender, System.EventArgs e) {/ / place user code here to initialize the page this.CreateCheckCodeImage (GenerateCheckCode ()) Code generated by Web forms designer # code generated by region Web forms designer override protected void OnInit (EventArgs e) {/ CODEGEN: this call is required by the asp.net Web forms designer. / / InitializeComponent (); base.OnInit (e);} / * /

< summary>

/ / the designer supports the required methods-do not use the code editor to modify the contents of this method. / / /

< /summary>

Private void InitializeComponent () {this.Load + = new System.EventHandler (this.Page_Load);} # endregion private string GenerateCheckCode () {int number; char code; string checkCode = String.Empty System.Random random = new Random (); for (int iTuno; I < 5; iSum +) {/ / randomly generate an integer number = random.Next () / / if the random number is even, choose from [0-9] if (number% 2 = = 0) code = (char) ('0' + (char) (number% 10)) Else / / if the random number is odd, choose from [A murz] code = (char) ('A'+ (char) (number% 26)) CheckCode + = code.ToString ();} Response.Cookies.Add (new HttpCookie ("CheckCode", checkCode); return checkCode } / / create a random graph private void CreateCheckCodeImage (string checkCode) {if (checkCode = = null | | checkCode.Trim () = = String.Empty) return / / establish a bitmap file to establish length and width System.Drawing.Bitmap image = new System.Drawing.Bitmap ((int) Math.Ceiling ((checkCode.Length * 12.5)), 22); Graphics g = Graphics.FromImage (image) Try {/ / generate random generator Random random = new Random () / / clear the background color of the picture g.Clear (Color.White); / / the background noise of the picture is for (int iTuno; I < 60). Image.Width +) {int x = random.Next (image.Width); int y = random.Next (image.Height) Image.SetPixel (x, y, Color.FromArgb (random.Next () } / / write the generated random number to the screen Font font = new System.Drawing.Font in font form ("Arial", 12, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic)) System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush (new Rectangle (0,0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true); g.DrawString (checkCode, font, brush, 2,2) / / draw the border of the picture g.DrawRectangle (new Pen (Color.Silver), 0,0, image.Width-1, image.Height-1); System.IO.MemoryStream ms = new System.IO.MemoryStream () Image.Save (ms, System.Drawing.Imaging.ImageFormat.Gif); Response.ClearContent (); Response.ContentType = "image/Gif" Response.BinaryWrite (ms.ToArray ());} finally {g.Dispose () Image.Dispose ();}}

Analysis of main functions of ASP.NET1.1 CAPTCHA

1, through the random function (Random) to generate the verification code component elements (here are five digits) and convert it into a string (the attribute is read-only), and then write it into "Cookie" for verification.

2. Write the CAPTCHA string to the drawing:

(1) create a bitmap file to determine the length and width:

System.Drawing.Bitmap image = new System.Drawing.Bitmap ((int) Math.Ceiling (checkCode.Length * 12.5), 22)

(a) System.Drawing. [C #] Bitmap (int width,int height)

(B) double Math.Ceiling (double a): returns the smallest integer greater than or equal to the specified number.

(2) the background noise of the picture (60):

For (int iTun0; I < 60; iTunes +) {int x = random.Next (image.Width); int y = random.Next (image.Height); image.SetPixel (x, y, Color.FromArgb (random.Next ();}

(a) public virtual int Next (int maxValue); returns a non-negative random number that is less than the specified * value. Parameter: the upper limit of the random number to be generated by maxValue-. MaxValue must be greater than or equal to zero.

(B) image.SetPixel (int x recording int yreign Color color); parameters: X-the x coordinate of the pixel to be set; y-the y coordinate of the pixel to be set; color-Color structure, which represents the color to be assigned to the specified pixel.

(C) the Color.FromArgb (int argb) parameter: argb- specifies the value of the 32-bit ARGB value.

(3) write the generated random number to the bitmap in the form of font: Graphics.DrawString (string sMagneFont font,Brush brush,float x float y)

Parameter: s-the string to be drawn; the font-Font object, which defines the text format of the string

A brush-Brush object that determines the color and texture of the text being drawn

X-the x coordinate of the upper-left corner of the drawn text

Y-the y coordinate of the upper-left corner of the drawn text. (draws the specified text string at the specified location and with the specified Brush and Font objects)

(4) draw the border of the picture: public void DrawRectangle (Pen pen, int x, int y, int width, int height); draw a rectangle specified by coordinate pair, width and height.

Parameters: pen-Pen object, which determines the color, width, and style of the rectangle

X-the x coordinate of the upper left corner of the rectangle to be drawn

Y-the y coordinate of the upper left corner of the rectangle to be drawn

The width of the rectangle to be drawn by width- and the height of the rectangle to be drawn by height-.

(5) output the picture as a binary stream and format it and display it.

After reading the above, do you have any further understanding of the principle and application of ASP.NET1.1 CAPTCHA? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report