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 are the ASP.NET verification codes?

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article introduces the relevant knowledge of "what are the ASP.NET CAPTCHA codes?". In the operation of actual cases, many people will encounter such a dilemma. Then let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1.GSC_WebControlLibrary this is a control found on the Internet, very easy to use. But the effect is not very good (see the picture below.

Although it is easy to use and all properties can be set like controls, the availability is not very high Users cannot customize it, and this CAPTCHA doesn't seem to work very well.

Effect:

two。 Generate a picture with one page, call another page, store the CAPTCHA in cookie, and compare it with cookie when calling. The user can change the effect and the length of the CAPTCHA according to his preference.

The effect is as shown in the figure:

The CheckCode.aspx code is as follows:

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;public partial class Tools_CheckCode: System.Web.UI.Page {protected void Page_Load (object sender, EventArgs e) {this.CreateCheckCodeImage (GenerateCheckCode ()) } private string GenerateCheckCode () {int number; char code; string checkCode = String.Empty; System.Random random = new Random (); for (int I = 0; I

< 5; i++) { 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)); return checkCode; } private void CreateCheckCodeImage(string checkCode) { if (checkCode == null || checkCode.Trim() == String.Empty) return; System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 22); Graphics g = Graphics.FromImage(image); try { //生成随机生成器 Random random = new Random(); //清空图片背景色 g.Clear(Color.White); //画图片的背景噪音线 for (int i = 0; i < 25; i++) { 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.GreenYellow), x1, y1, x2, y2); } Font font = new System.Drawing.Font("Verdana", 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); //画图片的前景噪音点 for (int i = 0; i < 80; i++) { int x = random.Next(image.Width); int y = random.Next(image.Height); image.SetPixel(x, y, Color.FromArgb(random.Next())); } //画图片的边框线 g.DrawRectangle(new Pen(Color.Red), 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(); } }} 然后在需要使用的页面引用: UseCheckCode.aspx 验证码

3. Use web handler to generate pictures. This is about the same as the previous meaning, and the calling method is basically the same as 2, except that its CAPTCHA is stored in Session. For learning and reference.

The effect picture is as follows:

ValidateImageHandler.ashx

% @ WebHandler Language= "C#" Class= "ValidateImageHandler"% > using System;using System.Web;using System.Web.SessionState;using System.Drawing;using System.Drawing.Imaging;using System.Text;/// ValidateImageHandler generate website CAPTCHA function / public class ValidateImageHandler: IHttpHandler, IRequiresSessionState {int intLength = 5; / / length string strIdentify = "Identify" / / Random string stores key values to store in Session public ValidateImageHandler () {} / generate verification image core code / public void ProcessRequest (HttpContext hc) {/ / set output stream image format hc.Response.ContentType = "image/gif"; Bitmap b = new Bitmap (200,60); Graphics g = Graphics.FromImage (b) G.FillRectangle (new SolidBrush (Color.YellowGreen), 0,0,200,60); Font font = new Font (FontFamily.GenericSerif, 48, FontStyle.Bold, GraphicsUnit.Pixel); Random r = new Random (); / / legal random display character list string strLetters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"; StringBuilder s = new StringBuilder (); / / draw randomly generated strings to for (int I = 0; I < intLength) ) {s.Append (strLetters.Substring (r.Next (0, strLetters.Length-1), 1); g.DrawString (s [s length-1] .ToString (), font, new SolidBrush (Color.Blue), I * 38, r.Next (0,15));} / / generate interference lines Pen pen = new Pen (new SolidBrush (Color.Blue), 2); for (int I = 0; I < 10) G.DrawLine +) {g.DrawLine (pen, new Point (r.Next (0199), r.Next (059)), new Point (r.Next (0199), r.Next (059));} b.Save (hc.Response.OutputStream, ImageFormat.Gif); hc.Session [strIdentify] = s.ToString (); / / first save in Session to verify whether it is consistent with user input hc.Response.End () } / indicates whether such instances can be shared by multiple requests (reuse can improve performance) / public bool IsReusable {get {return true;}} "what are the ASP.NET verification codes"? thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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