TOP

项目中验证码的使用(一)
2017-10-16 18:19:49 】 浏览:4621
Tags:项目 验证 使用

aspx类的验证码处理程序,随后还会记录一般程序的的验证码类

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Web;
 4 using System.Web.UI;
 5 using System.Web.UI.WebControls;
 6 using System.Drawing;
 7 using Maticsoft.Common;
 8 namespace Maticsoft.Web
 9 {
10     public partial class ValidateCode : System.Web.UI.Page
11     {
12         private static char[] constant =    //声明一个静态的字符数组,验证码从这里面随机的取
13       {   
14         '1','2','3','4','5','6','7','8','9',
15         'a','b','c','d','e','f','g','h','i','j','k','l','m','n','p','q','r','s','t','u','v','w','x','y','z',   
16         'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'   
17       };
18         private void Page_Load(object sender, System.EventArgs e)
19         {
20             string checkCode = CreateNumber(4);
21           //  Session["validateCode"] = checkCode.ToLower()
22             Common_Sesson.Add("validateCode", checkCode.ToLower());
23             CreateCheckCodeImage(checkCode);
24         }
25 
26         #region 画出图片并显示到调用这个方法的画布上
27         private void CreateCheckCodeImage(string code)
28         {
29             var imageCode = new System.Drawing.Bitmap((int)Math.Ceiling((code.Length * 12.5)), 23); //定义图片的宽度和高度
30             var g = Graphics.FromImage(imageCode);  //加载图片到画布上
31 
32             HttpContext.Current.Session["Code_Char"] = code; //将验证码写入到session中实现验证
33 
34             try
35             {
36                 var random = new Random();
37                 g.Clear(Color.White); //清空图片背景色
38 
39                 //画图片的背景噪音线
40                 for (int i = 0; i < 25; i++)
41                 {
42                     var x1 = random.Next(imageCode.Width);
43                     var x2 = random.Next(imageCode.Width);
44                     var y1 = random.Next(imageCode.Height);
45                     var y2 = random.Next(imageCode.Height);
46 
47                     g.DrawLine(new Pen(Color.Silver), new Point(x1, y1), new Point(x2, y2));
48                 }
49 
50                 var font = new System.Drawing.Font("Arial", 12F, System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic);
51                 var brush = new System.Drawing.Drawing2D.LinearGradientBrush(
52                     new Rectangle(0, 0, imageCode.Width, imageCode.Height),
53                     Color.Blue, Color.DarkRed, 1.2F, true);
54                 g.DrawString(code, font, brush, 2, 2);
55 
56                 //画图片的前景噪音点
57                 for (int i = 0; i < 100; i++)
58                 {
59                     var x = random.Next(imageCode.Width);
60                     var y = random.Next(imageCode.Height);
61                     imageCode.SetPixel(x, y, Color.FromArgb(random.Next()));
62                 }
63 
64                 //画图片的边框线
65                 g.DrawRectangle(new Pen(Color.Silver), 0, 0, imageCode.Width - 1, imageCode.Height - 1);
66                 var ms = new System.IO.MemoryStream();
67                 imageCode.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
68 
69                 HttpContext.Current.Response.ClearContent();
70                 HttpContext.Current.Response.ContentType = "image/jpg";
71                 HttpContext.Current.Response.BinaryWrite(ms.ToArray());
72             }
73             finally
74             {
75                 g.Dispose();
76                 imageCode.Dispose();
77             }
78         }
79         #endregion
80         #region 生成0——60的随机数
81         private static string CreateNumber(int n) //n表示产生字符串的长度
82         {
83             string str = "";
84             System.Text.StringBuilder newRandom = new System.Text.StringBuilder(60);
85             Random rd = new Random();
86             while (n-- > 0)
87             {
88                 str = str + constant[rd.Next(60)];
89             }
90             return str;
91         }
92         #endregion
93 
94 
95     }
96 }
View Code

 

前台的视图代码

1 <p>验证码: <input type="text" id="yzm" style=" width:100px"/>
2          <img title="看不清?" style=" cursor:pointer ;vertical-align:middle" width="70px"  src="Valid  
		
项目中验证码的使用(一) https://www.cppentry.com/bencandy.php?fid=106&id=164988

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇基于EF Core的Code First模式的Do.. 下一篇asp.net mvc CodeFirst模式数据库..