关于所有登陆相关的验证码模块的剖析及源代码
来源:互联网 发布:xp系统添加网络打印机 编辑:程序博客网 时间:2024/06/03 21:15
1. 简洁版,直接生成数字。
在label或者textbox中生成简单数字即可,主要是调用Random函数:
Random validateN = new Random();
this.Label1.Text = validateN.Next(9).ToString() + validateN.Next(9).ToString() + validateN.Next(9).ToString() + validateN.Next(9).ToString();
当点击登录按钮时,把输入的验证码与生成的验证码进行比较即可:
if (this.ValidateNumber.Text != this.Label1.Text)
{
Response.Write("<script>alert('验证码错误');history.back()</script>");
return;
}
截图如下:
补充:Random函数的Next()方法有三种重载形式。
2. 复杂版,通过GDI绘图生成字母。
步骤一:专门写一个绘图类,以后可以永久使用。
代码如下:
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.Imaging;
using System.Drawing.Drawing2D;
using System.Drawing;
public partial class GDI : System.Web.UI.Page
{
// 验证码长度
private int codeLen = 4;
// 图片清晰度
private int fineness = 100;
// 图片宽度
private int imgWidth = 100;
// 图片高度
private int imgHeight = 24;
// 字体家族名称
private string fontFamily = "Arial Black";
// 字体大小
private int fontSize = 18;
// 字体样式
private int fontStyle = 0;
// 绘制起始坐标 X
private int posX = 0;
// 绘制起始坐标 Y
private int posY = 0;
//------------------------------------------------------------
// code.aspx 页面加载函数
//------------------------------------------------------------
protected void Page_Load(object sender, EventArgs e)
{
#region 读取 Request 传递参数
// 获取代码长度设置
if (Request["CodeLen"] != null)
{
try
{
codeLen = Int32.Parse(Request["CodeLen"]);
// 规定验证码长度
if (codeLen < 4 || codeLen > 16)
throw new Exception("验证码长度必须在4到16之间");
}
catch (Exception Ex)
{
throw Ex;
}
}
// 获取图片清晰度设置
if (Request["Fineness"] != null)
{
try
{
fineness = Int32.Parse(Request["Fineness"]);
// 验证清晰度
if (fineness < 0 || fineness > 100)
throw new Exception("图片清晰度必须在0到100之间");
}
catch (Exception Ex)
{
throw Ex;
}
}
// 获取图片宽度
if (Request["ImgWidth"] != null)
{
try
{
imgWidth = Int32.Parse(Request["ImgWidth"]);
if (imgWidth < 16 || imgWidth > 480)
throw new Exception("图片宽度必须在16到480之间");
}
catch (Exception Ex)
{
throw Ex;
}
}
// 获取图片高度
if (Request["ImgHeight"] != null)
{
try
{
imgHeight = Int32.Parse(Request["ImgHeight"]);
if (imgHeight < 16 || imgHeight > 320)
throw new Exception("图片高度必须在16到320之间");
}
catch (Exception Ex)
{
throw Ex;
}
}
// 获取验证码字体家族名称
if (Request["FontFamily"] != null)
{
fontFamily = Request["FontFamily"];
}
// 获取验证码字体大小
if (Request["FontSize"] != null)
{
try
{
fontSize = Int32.Parse(Request["FontSize"]);
if (fontSize < 8 || fontSize > 72)
throw new Exception("字体大小必须在8到72之间");
}
catch (Exception Ex)
{
throw Ex;
}
}
// 获取字体样式
if (Request["FontStyle"] != null)
{
try
{
fontStyle = Int32.Parse(Request["FontStyle"]);
}
catch (Exception Ex)
{
throw Ex;
}
}
// 验证码绘制起始位置 X
if (Request["PosX"] != null)
{
try
{
posX = Int32.Parse(Request["PosX"]);
}
catch (Exception Ex)
{
throw Ex;
}
}
// 验证码绘制起始位置 Y
if (Request["PosY"] != null)
{
try
{
posY = Int32.Parse(Request["PosY"]);
}
catch (Exception Ex)
{
throw Ex;
}
}
#endregion
string code = Createcode();
// 生成BITMAP图像
Bitmap bitmap = new Bitmap(imgWidth, imgHeight);
// 给图像设置干扰
DisturbBitmap(bitmap);
// 绘制验证码图像
Drawcode(bitmap, code);
// 保存验证码图像,等待输出
bitmap.Save(Response.OutputStream, ImageFormat.Gif);
//string targetFile = Server.MapPath("~/image/") + "Pic.gif";
//this.Image1.ImageUrl = targetFile; }
//------------------------------------------------------------
// 随机生成验证码,并保存到SESSION中
//------------------------------------------------------------
private string Createcode()
{
string code = "";
// 随机数对象
Random random = new Random();
for (int i = 0; i < codeLen; i++)
{
// 26: a - z
int n = random.Next(26);
// 将数字转换成大写字母
code += (char)(n + 65);
}
// 保存验证码
Session["code"] = code;
return code;
}
//------------------------------------------------------------
// 为图片设置干扰点
//------------------------------------------------------------
private void DisturbBitmap(Bitmap bitmap)
{
// 通过随机数生成
Random random = new Random();
for (int i = 0; i < bitmap.Width; i++)
{
for (int j = 0; j < bitmap.Height; j++)
{
if (random.Next(100) <= this.fineness)
bitmap.SetPixel(i, j, Color.White);
}
}
}
//------------------------------------------------------------
// 绘制验证码图片
//------------------------------------------------------------
private void Drawcode(Bitmap bitmap, string code)
{
// 获取绘制器对象
Graphics g = Graphics.FromImage(bitmap);
// 设置绘制字体
Font font = new Font(fontFamily, fontSize, GetFontStyle());
Pen myPen = new Pen(Color.Black, 2);
//随机画两条线
Random r = new Random();
float x1 = r.Next(imgWidth);
float x2 = r.Next(imgWidth);
float y1 = r.Next(imgHeight);
float y2 = r.Next(imgHeight);
g.DrawLine(myPen, x1, y1, x2, y2);
x1 = r.Next(imgWidth);
x2 = r.Next(imgWidth);
y1 = r.Next(imgHeight);
y2 = r.Next(imgHeight);
g.DrawLine(myPen, x1, y1, x2, y2);
// 绘制验证码图像
g.DrawString(code, font, Brushes.Black, posX, posY);
}
//------------------------------------------------------------
// 换算验证码字体样式:1 粗体 2 斜体 3 粗斜体,默认为普通字体
//------------------------------------------------------------
private FontStyle GetFontStyle()
{
if (fontStyle == 1)
return FontStyle.Bold;
else if (fontStyle == 2)
return FontStyle.Italic;
else if (fontStyle == 3)
return FontStyle.Bold | FontStyle.Italic;
else
return FontStyle.Regular;
}
}
步骤二:对步骤一创建的public partial class GDI文件进行引用,其代码如下:
脚本:
<script language=javascript>
function refresh()
{
var url="GDI.aspx?id=";
var r=Math.random()*1000;
url=url+r;
document.all("LoI_ValidateCodeImage").src=url;
document.all("LoI_txtVC").value="";
document.all("LoI_txtVC").focus();
return false;
}
</script>
网页代码:
<img src="GDI.aspx" id="LoI_ValidateCodeImage" onclick="refresh();" height="25" border="4" style="cursor:pointer;border:0px solid; vertical-align:top;" />
<a href="javascript:refresh();" >点击刷新验证码</a>
效果图:
能力拔高:
如果需要出现字符加数字混合型的验证码只需在
for (int i = 0; i < codeLen; i++)
{
// 26: a - z
int n = random.Next(26);
// 将数字转换成大写字母
code += (char)(n + 65);
}
处修改即可。
修改后代码如下:
for (int i = 0; i < codeLen; i++)
{
// 26: a - z
int n = random.Next(26);
// 将数字转换成大写字母
code += (char)(n + 65) + random.Next(9).ToString();
}
效果图:
3. 通过小型数据文件存储(如Access数据文件),然后每次循环调用生成汉字,但数据库文件得加密,以确保黑客获得也无法读取数据。
此种方法思路比较清晰,但代码量较大且安全性不是很高(个人意见),一般不是很实用。故在此就不展开大讨论了。网上也可以下到此类源代码。
- 关于所有登陆相关的验证码模块的剖析及源代码
- Java登陆验证模块的实现及小结
- 关于验证码的相关文章
- 关于Apache的登陆验证配置
- 登陆用的验证码
- 登陆时的验证码
- 登陆验证码的生成
- 登陆验证码的实现
- hash_map的数据结构剖析及相关方法
- 验证码的java源代码
- PHP (thinkphp)后台设置验证登陆(所有)和登陆超时的方法
- 登陆模块的描述
- 关于我的第一个项目的登陆模块心得
- 关于登陆验证中字符串的比较的小阴沟
- 关于tinyxml的剖析及遍历
- 关于tinyxml的剖析及遍历
- 关于tinyxml的剖析及遍历
- 关于登陆模块的一些测试用例
- mysql常用语句
- 使用PowerDesigner 建立企业知识库
- java面试题及答案(基础题122道,代码题19道)2009-8-6-004中部分
- 索引問題求解
- 高精度计算----减法运算
- 关于所有登陆相关的验证码模块的剖析及源代码
- 笑话收藏
- VB调用vc写的dll的某个方法时的问题
- java面试题及答案(基础题122道,代码题19道)2009-8-6-004下部分
- 这个项目总算是告一段落了
- 从PowerDesigner概念设计模型(CDM)中的3种实体关系说起
- ndisprot代码中的调试信息问题
- java面试题及答案(基础题122道,代码题19道)2009-8-6-004续
- C#获取本机计算机名,IP地址和MAC地址