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 make website CAPTCHA

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

Share

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

This article introduces you how to make the verification code of the website, the content is very detailed, interested friends can refer to, I hope it can be helpful to you.

Production of asp.net CAPTCHA

Using System

Using System.IO

Using System.Drawing

Using System.Drawing.Imaging

Using System.Text

Using System.Collections

Using System.Web

Using System.Web.UI

Using System.Web.UI.WebControls

Using System.Web.UI.WebControls.WebParts

Using System.Web.UI.HtmlControls

Namespace WebApplication1

{

Public partial class yzm: System.Web.UI.Page

{

Protected void Page_Load (object sender, EventArgs e)

{

String chkCode = string.Empty

/ / Color list, which is used for verification code, noise line, noise point

Color [] color = {Color.Black, Color.Red, Color.Blue, Color.Green, Color.Orange, Color.Brown, Color.Brown, Color.DarkBlue}

/ / Font list, used for CAPTCHA

String [] font = {"Times New Roman", "MS Mincho", "Book Antiqua", "Gungsuh", "PMingLiU", "Impact"}

/ / the character set of the CAPTCHA, removing some characters that are easy to be confused

Char [] character = {'2, 3, 4, 5, 6, 8, 9, A, B, C, D, E, F, G, H, J, K, L, M, N, P, R, S, T, W' 'Xuan,'Y'}

Random rnd = new Random ()

/ / generate a CAPTCHA string

For (int I = 0; I < 4; iTunes +)

{

ChkCode + = "[rnd.Next (character.Length)]

}

/ / use session object to store CAPTCHA

Session ["name"] = chkCode

Bitmap bmp = new Bitmap (100,40)

Graphics g = Graphics.FromImage (bmp)

G.Clear (Color.White)

/ / draw noise lines

For (int I = 0; I < 10; iTunes +)

{

Int x1 = rnd.Next (100)

Int Y1 = rnd.Next (40)

Int x2 = rnd.Next (100)

Int Y2 = rnd.Next (40)

Color clr = color [rnd.Next (color.Length)]

G.DrawLine (new Pen (clr), x1, y1, x2, y2)

}

/ / draw CAPTCHA string

For (int I = 0; I < chkCode.Length; iTunes +)

{

String fnt = Font [rnd.Next (font.Length)]

Font ft = new Font (fnt, 18)

Color clr = color [rnd.Next (color.Length)]

G.DrawString (chkCode [I] .ToString (), ft, new SolidBrush (clr), (float) I * 20 + 8, (float) 8)

}

/ / draw noise

For (int I = 0; I < 100; iTunes +)

{

Int x = rnd.Next (bmp.Width)

Int y = rnd.Next (bmp.Height)

Color clr = color [rnd.Next (color.Length)]

Bmp.SetPixel (x, y, clr)

}

/ / clear the page output cache and set the page to have no cache

Response.Buffer = true

Response.ExpiresAbsolute = System.DateTime.Now.AddMilliseconds (0)

Response.Expires = 0

Response.CacheControl = "no-cache"

Response.AppendHeader ("Pragma", "No-Cache")

/ / write the CAPTCHA image to the memory stream and output it in "p_w_picpath/Png" format

MemoryStream ms = new MemoryStream ()

Try

{

Bmp.Save (ms, ImageFormat.Png)

Response.ClearContent ()

Response.ContentType = "p_w_picpath/Png"

Response.BinaryWrite (ms.ToArray ())

}

Finally

{

/ / explicitly release resources

Bmp.Dispose ()

G.Dispose ()

}

}

}

}

About how to carry on the website CAPTCHA production to share here, hope that the above content can have some help to everyone, can learn more knowledge. If you think the article is good, you can share it for more people to see.

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