当前位置: 移动技术网 > IT编程>开发语言>.net > ASP.NET在实际开发中验证码的用法

ASP.NET在实际开发中验证码的用法

2018年01月30日  | 移动技术网IT编程  | 我要评论
在网上有看到很多关于验证码的代码,很多都只是生成一张验证码图片,然而在实际登陆验证模块,验证码要怎么添加进去或者说怎么运用、和实际项目开发中要怎么使用验证码,我自己总结了几点。
在网上有看到很多关于验证码的代码,很多都只是生成一张验证码图片,然而在实际登陆验证模块,验证码要怎么添加进去或者说怎么运用、和实际项目开发中要怎么使用验证码,我自己总结了几点。

 

一、在实际开发登陆模块的验证码,程序员是将验证码的文本值(字符串)存在Session中的,然后在登陆验证的时候,通过Session取值进行判断的,这样效率会高很多。

 

二、然而在写验证码的时候要想通过Session存值,就必须实现System.Web.SessionState.IRequiresSessionState这个接口

 

三、以一般处理程序(ashx页面)为列,下面对验证码写法和运用进行详解

 

代码:

 

复制代码

  1 using System;

  2 using System.Collections.Generic;

  3 using System.Drawing;

  4 using System.Linq;

  5 using System.Web;

  6 

  7 namespace vcodeDemo

  8 {

  9     /// <summary>

 10     /// vcode 写法的说明

 11     /// </summary>

 12     public class c01vcode : IHttpHandler,System.Web.SessionState.IRequiresSessionState

 13     //如果要在一般处理程序中能够正常使用session则必须实现IRequiresSessionState接口

 14     {

 15         public void ProcessRequest(HttpContext context)

 16         {

 17             //1 设置ContentType为图片类型

 18             context.Response.ContentType = "image/jpeg";

 19 

 20             //2 准备要作画的图片对象,宽度为80 高度为25  ,Bitmap:位图

 21             using (Image img = new Bitmap(80, 25))

 22             {

 23                 // 从img对象上定义画家

 24                 using (Graphics g = Graphics.FromImage(img))

 25                 {

 26                     //以白色来清除位图的背景

 27                     g.Clear(Color.White);

 28 

 29                     //画图片的边框为红色,从左上角开始画满整个图片

 30                     g.DrawRectangle(Pens.Red, 0, 0, img.Width - 1, img.Height - 1);

 31 

 32                     //在验证码文字前面画50个噪点

 33                     this.DrawPoint(50, g, img.Width, img.Height);

 34 

 35                     //得到验证码文本字符串(随机产生4个字符)

 36                     string vcode = this.GetVCode(4);

 37 

 38                     //保存验证码文本字符串到session中

 39                     context.Session["vcode"] = vcode;

 40 

 41                     //将验证码字符串写入到图片对象上

 42                     g.DrawString(vcode

 43                         , new Font("Arial", 16, FontStyle.Strikeout | FontStyle.Bold) // 给文本加中横线和加粗

 44                         , new SolidBrush(Color.Red)

 45                         , new PointF(r.Next(15), r.Next(8))

 46                         );

 47 

 48                     //在验证码文字后面画50个噪点

 49                     this.DrawPoint(50, g, img.Width, img.Height);

 50                 }

 51                 //将验证码输出给浏览器                

 52                 img.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);

 53             }

 54         }

 55 

 56         /// <summary>

 57         /// 在图片对象上画噪点

 58         /// </summary>

 59         /// <param name="count"></param>

 60         void DrawPoint(int count, Graphics g, int width, int height)

 61         {

 62             for (int i = 0; i < count; i++)

 63             {

 64                 int x = r.Next(width);

 65                 int y = r.Next(height);

 66 

 67                 g.DrawLine(Pens.Blue

 68                     , new Point(x, y)

 69                     , new Point(x + 2, y + 2)

 70                     );

 71             }

 72         }

 73 

 74         /// <summary>

 75         /// 定义产生随机数的对象

 76         /// </summary>

 77         Random r = new Random();

 78 

 79         /// <summary>

 80         /// 产生验证码文本字符串

 81         /// </summary>

 82         /// <param name="count"></param>

 83         /// <returns></returns>

 84         string GetVCode(int count)

 85         {

 86             //声明返回值

 87             string rescode = "";

 88             string codestr = "ABCDabcd123456789";

 89             char[] codeArr = codestr.ToArray();

 90             for (int i = 0; i < count; i++)

 91             {

 92                 rescode += codeArr[r.Next(codestr.Length)];

 93             }

 94             //返回字符串

 95             return rescode;

 96         }

 97 

 98         public bool IsReusable

 99         {

100             get

101             {

102                 return false;

103             }

104         }

105     }

106 }

复制代码

四、在验证登陆判断的时候,因为我们通过上下文对象的Session给验证码文本赋值并存入Session中去: context.Session["vcode"] = vcode;所有在进行验证的时候可以使用Session["vcode"]进行取值,然后进行判断。

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网