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

How to generate ASP.NET CAPTCHA

2025-04-13 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces how to generate ASP.NET CAPTCHA, which is very detailed and has a certain reference value. Friends who are interested must read it!

Step 1: randomly pick out the numbers or letters of a system CAPTCHA, and write the randomly generated numbers or letters into Cookies or Session.

Step 2: use the random numbers or letters in the first step to synthesize the picture.

You can see that the complexity of the CAPTCHA is mainly completed in the second step, and you can set it according to the complexity you want.

Let's take a look at this:

Step 1: the method of randomly generating numbers or letters

/ / the random number that generates the CAPTCHA / returns the five-digit random number private string GenerateCheckCode () {int number; char code; string checkCode = String.Empty; Random random = new Random (); for (int I = 0; I < 5; iCAPTCHA +) / / you can arbitrarily set the number of digits {number = random.Next () If (number% 2 = = 0) code = (char) ('0' + (char) (number% 10)); else code = (char) ('A' + (char) (number% 26)); checkCode + = code.ToString ();} Response.Cookies.Add (new HttpCookie ("CheckCode", checkCode)); / / write COOKIS Session ["CheckCode"] = checkCode; / / write Session, optionally return checkCode;}

Step 2: generate pictures

/ / generate CAPTCHA picture / private void CreateCheckCodeImage (string checkCode) {if (checkCode = = null | | checkCode.Trim () = = String.Empty) return; Bitmap image = new Bitmap ((int) Math.Ceiling ((checkCode.Length * 12.5)), 22); Graphics g = Graphics.FromImage (image); try {/ / generate random generator Random random = new Random () / / clear background color g.Clear (Color.White); / / background noise line for (int I = 0; I < 25; iTunes +) {int x1 = random.Next (image.Width); int x2 = random.Next (image.Width); int Y1 = random.Next (image.Height); int y2 = random.Next (image.Height) G.DrawLine (new Pen (Color.Silver), x1, y1, x2, y2);} Font font = new System.Drawing.Font ("Arial", 12, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic)); LinearGradientBrush brush = new LinearGradientBrush (new Rectangle (0,0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true); g.DrawString (checkCode, font, brush, 2,2) / / foreground noise point for (int I = 0; I < 100; iTunes +) {int x = random.Next (image.Width); int y = random.Next (image.Height); image.SetPixel (x, y, Color.FromArgb (random.Next () } / / draw the border of the picture g.DrawRectangle (new Pen (Color.Silver), 0,0, image.Width-1, image.Height-1); MemoryStream ms = new MemoryStream (); image.Save (ms, System.Drawing.Imaging.ImageFormat.Gif); Response.ClearContent (); Response.ContentType = "image/Gif"; Response.BinaryWrite (ms.ToArray ()) } finally {/ / release object resources g.Dispose (); image.Dispose ();}

* complete program

First add a checkCode.aspx file to the project in VS2005, and add the following complete code to the checkCode.aspx.cs code file

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.IO;using System.Drawing.Drawing2D;public partial class checkCode: System.Web.UI.Page {protected void Page_Load (object sender, EventArgs e) {CreateCheckCodeImage (GenerateCheckCode ()); / / call the following two methods } / the random number that generates the CAPTCHA / returns the five-digit random number private string GenerateCheckCode () {int number; char code; string checkCode = String.Empty; Random random = new Random (); for (int I = 0; I < 5; iCAPTCHA +) / / you can arbitrarily set the number of digits {number = random.Next () of the generated CAPTCHA. If (number% 2 = = 0) code = (char) ('0' + (char) (number% 10)); else code = (char) ('A' + (char) (number% 26)); checkCode + = code.ToString ();} Response.Cookies.Add (new HttpCookie ("CheckCode", checkCode)); / / write COOKIS Session ["CheckCode"] = checkCode; / / write Session, you can choose return checkCode at will } / private void CreateCheckCodeImage (string checkCode) {if (checkCode = = null | | checkCode.Trim () = = String.Empty) return; Bitmap image = new Bitmap ((int) Math.Ceiling ((checkCode.Length * 12.5)), 22); Graphics g = Graphics.FromImage (image); try {/ / generate random generator Random random = new Random () / / clear background color g.Clear (Color.White); / / background noise line for (int I = 0; I < 25; iTunes +) {int x1 = random.Next (image.Width); int x2 = random.Next (image.Width); int Y1 = random.Next (image.Height); int y2 = random.Next (image.Height) G.DrawLine (new Pen (Color.Silver), x1, y1, x2, y2);} Font font = new System.Drawing.Font ("Arial", 12, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic)); LinearGradientBrush brush = new LinearGradientBrush (new Rectangle (0,0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true); g.DrawString (checkCode, font, brush, 2,2) / / foreground noise point for (int I = 0; I < 100; iTunes +) {int x = random.Next (image.Width); int y = random.Next (image.Height); image.SetPixel (x, y, Color.FromArgb (random.Next () } / / draw the border of the picture g.DrawRectangle (new Pen (Color.Silver), 0,0, image.Width-1, image.Height-1); MemoryStream ms = new MemoryStream (); image.Save (ms, System.Drawing.Imaging.ImageFormat.Gif); Response.ClearContent (); Response.ContentType = "image/Gif"; Response.BinaryWrite (ms.ToArray ()) } finally {/ / release object resources g.Dispose (); image.Dispose ();}

The above page that generates the CAPTCHA is ready. Let's call:

Add an Image control where you need the CAPTCHA

Then the CAPTCHA will be displayed on the Image control!

The display is ready, of course, we have to judge whether the user's input is correct!

As long as we get the value entered by the user and compare it with Cookis or Session, it will be OK.

Take the value of Cookies Request.Cookies ["CheckCode"] .Value

Take the value of Session Session ["CheckCode"] .ToString () (it is best to determine whether Session is empty first)

If you are not case-sensitive, convert the value entered by the user and the value of Cookies or Session to uppercase or lowercase

Supplementary method

Protected void Button1_Click (object sender, EventArgs e) {if (Request.Cookies ["CheckCode"]. Value = = TextBox1.Text.Trim () .ToString () {Response.Write ("Cookies is right");} else {Response.Write ("Cookies is wrong") } if (Session ["CheckCode"]! = null) {if (Session ["CheckCode"] .ToString () .ToUpper () = = TextBox1.Text.Trim () .ToString () .ToUpper ()) / / this can not be case-sensitive {Response.Write ("Session is right");} else {Response.Write ("Session is wrong") } the above is all the content of this article "how to generate ASP.NET CAPTCHA". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

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