代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Asp.Net
】
数字验证码
作者:
Dezai.CN
/ 发布于
2011/7/8
/
839
<div><span style="color: rgb(0,0,255)">protected</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">void</span><span style="color: rgb(0,0,0)"> Page_Load(</span><span style="color: rgb(0,0,255)">object</span><span style="color: rgb(0,0,0)"> sender, EventArgs e) { HttpContext.Current.Session[</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">CheckCode</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(0,0,0)">]</span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)">RndNum(</span><span style="color: rgb(128,0,128)">4</span><span style="color: rgb(0,0,0)">); checkCodes(HttpContext.Current.Session[</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">CheckCode</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(0,0,0)">].ToString()); }</span></div> <div></div> <div><span style="color: rgb(0,0,0)"><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"><summary></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> 生成随机的字母 </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"></summary></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"><param name="VcodeNum"></span><span style="color: rgb(0,128,0)">生成字母的个数</span><span style="color: rgb(128,128,128)"></param></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"><returns></span><span style="color: rgb(0,128,0)">string</span><span style="color: rgb(128,128,128)"></returns></span><span style="color: rgb(128,128,128)"> </span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">private</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> RndNum(</span><span style="color: rgb(0,0,255)">int</span><span style="color: rgb(0,0,0)"> VcodeNum) { </span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> Vchar </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">0,1,2,3,4,5,6,7,8,9</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(0,0,0)">; </span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)">[] VcArray </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> Vchar.Split(</span><span style="color: rgb(128,0,0)">'</span><span style="color: rgb(128,0,0)">,</span><span style="color: rgb(128,0,0)">'</span><span style="color: rgb(0,0,0)">); </span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> VNum </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,0)">""</span><span style="color: rgb(0,0,0)">; </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">由于字符串很短,就不用StringBuilder了</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">int</span><span style="color: rgb(0,0,0)"> temp </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,0)">-</span><span style="color: rgb(128,0,128)">1</span><span style="color: rgb(0,0,0)">; </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">记录上次随机数值,尽量避免生产几个一样的随机数 </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">采用一个简单的算法以保证生成随机数的不同</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> Random rand </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">new</span><span style="color: rgb(0,0,0)"> Random(); </span><span style="color: rgb(0,0,255)">for</span><span style="color: rgb(0,0,0)"> (</span><span style="color: rgb(0,0,255)">int</span><span style="color: rgb(0,0,0)"> i </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,128)">1</span><span style="color: rgb(0,0,0)">; i </span><span style="color: rgb(0,0,0)"><</span><span style="color: rgb(0,0,0)"> VcodeNum </span><span style="color: rgb(0,0,0)">+</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,128)">1</span><span style="color: rgb(0,0,0)">; i</span><span style="color: rgb(0,0,0)">++</span><span style="color: rgb(0,0,0)">) { </span><span style="color: rgb(0,0,255)">if</span><span style="color: rgb(0,0,0)"> (temp </span><span style="color: rgb(0,0,0)">!=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,0)">-</span><span style="color: rgb(128,0,128)">1</span><span style="color: rgb(0,0,0)">) { rand </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">new</span><span style="color: rgb(0,0,0)"> Random(i </span><span style="color: rgb(0,0,0)">*</span><span style="color: rgb(0,0,0)"> temp </span><span style="color: rgb(0,0,0)">*</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">unchecked</span><span style="color: rgb(0,0,0)">((</span><span style="color: rgb(0,0,255)">int</span><span style="color: rgb(0,0,0)">)DateTime.Now.Ticks)); } </span><span style="color: rgb(0,0,255)">int</span><span style="color: rgb(0,0,0)"> t </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> rand.Next(VcArray.Length); </span><span style="color: rgb(0,0,255)">if</span><span style="color: rgb(0,0,0)"> (temp </span><span style="color: rgb(0,0,0)">!=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,0)">-</span><span style="color: rgb(128,0,128)">1</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,0)">&&</span><span style="color: rgb(0,0,0)"> temp </span><span style="color: rgb(0,0,0)">==</span><span style="color: rgb(0,0,0)"> t) { </span><span style="color: rgb(0,0,255)">return</span><span style="color: rgb(0,0,0)"> RndNum(VcodeNum); } temp </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> t; VNum </span><span style="color: rgb(0,0,0)">+=</span><span style="color: rgb(0,0,0)"> VcArray[t]; } </span><span style="color: rgb(0,0,255)">return</span><span style="color: rgb(0,0,0)"> VNum; }</span> <span style="font-size: small" size="3"><span style="font-size: small" size="3"><font size="2">?</font></span></span> <div class="cnblogs_code"> <pre><div><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"><summary></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> 生成验证图片 </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"></summary></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"><param name="checkCode"></span><span style="color: rgb(0,128,0)">验证字符</span><span style="color: rgb(128,128,128)"></param></span><span style="color: rgb(128,128,128)"></span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">private</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">void</span><span style="color: rgb(0,0,0)"> checkCodes(</span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> checkCode) { </span><span style="color: rgb(0,0,255)">int</span><span style="color: rgb(0,0,0)"> iwidth </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> (</span><span style="color: rgb(0,0,255)">int</span><span style="color: rgb(0,0,0)">)(checkCode.Length </span><span style="color: rgb(0,0,0)">*</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,128)">13</span><span style="color: rgb(0,0,0)">); System.Drawing.Bitmap image </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">new</span><span style="color: rgb(0,0,0)"> System.Drawing.Bitmap(iwidth, </span><span style="color: rgb(128,0,128)">23</span><span style="color: rgb(0,0,0)">); Graphics g </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> Graphics.FromImage(image); g.Clear(Color.White); </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">定义颜色</span><span style="color: rgb(0,128,0)"></span><span style="color: rgb(0,0,0)"> Color[] c </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> { Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Orange, Color.Brown, Color.DarkCyan, Color.Purple }; </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">定义字体 </span><span style="color: rgb(0,128,0)"></span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)">[] font </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> { </span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">Verdana</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(0,0,0)">, </span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">Microsoft Sans Serif</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(0,0,0)">, </span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">Comic Sans MS</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(0,0,0)">, </span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">Arial</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(0,0,0)">, </span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">宋体</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(0,0,0)"> }; Random rand </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">new</span><span style="color: rgb(0,0,0)"> Random(); </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">随机输出噪点</span><span style="color: rgb(0,128,0)"></span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">for</span><span style="color: rgb(0,0,0)"> (</span><span style="color: rgb(0,0,255)">int</span><span style="color: rgb(0,0,0)"> i </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,128)">0</span><span style="color: rgb(0,0,0)">; i </span><span style="color: rgb(0,0,0)"><</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,128)">50</span><span style="color: rgb(0,0,0)">; i</span><span style="color: rgb(0,0,0)">++</span><span style="color: rgb(0,0,0)">) { </span><span style="color: rgb(0,0,255)">int</span><span style="color: rgb(0,0,0)"> x </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> rand.Next(image.Width); </span><span style="color: rgb(0,0,255)">int</span><span style="color: rgb(0,0,0)"> y </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> rand.Next(image.Height); g.DrawRectangle(</span><span style="color: rgb(0,0,255)">new</span><span style="color: rgb(0,0,0)"> Pen(Color.LightGray, </span><span style="color: rgb(128,0,128)">0</span><span style="color: rgb(0,0,0)">), x, y, </span><span style="color: rgb(128,0,128)">1</span><span style="color: rgb(0,0,0)">, </span><span style="color: rgb(128,0,128)">1</span><span style="color: rgb(0,0,0)">); } </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">输出不同字体和颜色的验证码字符</span><span style="color: rgb(0,128,0)"></span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">for</span><span style="color: rgb(0,0,0)"> (</span><span style="color: rgb(0,0,255)">int</span><span style="color: rgb(0,0,0)"> i </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,128)">0</span><span style="color: rgb(0,0,0)">; i </span><span style="color: rgb(0,0,0)"><</span><span style="color: rgb(0,0,0)"> checkCode.Length; i</span><span style="color: rgb(0,0,0)">++</span><span style="color: rgb(0,0,0)">) { </span><span style="color: rgb(0,0,255)">int</span><span style="color: rgb(0,0,0)"> cindex </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> rand.Next(</span><span style="color: rgb(128,0,128)">7</span><span style="color: rgb(0,0,0)">); </span><span style="color: rgb(0,0,255)">int</span><span style="color: rgb(0,0,0)"> findex </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> rand.Next(</span><span style="color: rgb(128,0,128)">5</span><span style="color: rgb(0,0,0)">); Font f </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">new</span><span style="color: rgb(0,0,0)"> System.Drawing.Font(font[findex], </span><span style="color: rgb(128,0,128)">10</span><span style="color: rgb(0,0,0)">, System.Drawing.FontStyle.Bold); Brush b </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">new</span><span style="color: rgb(0,0,0)"> System.Drawing.SolidBrush(c[cindex]); </span><span style="color: rgb(0,0,255)">int</span><span style="color: rgb(0,0,0)"> ii </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,128)">4</span><span style="color: rgb(0,0,0)">; </span><span style="color: rgb(0,0,255)">if</span><span style="color: rgb(0,0,0)"> ((i </span><span style="color: rgb(0,0,0)">+</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,128)">1</span><span style="color: rgb(0,0,0)">) </span><span style="color: rgb(0,0,0)">%</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,128)">2</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,0)">==</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,128)">0</span><span style="color: rgb(0,0,0)">) { ii </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,128)">2</span><span style="color: rgb(0,0,0)">; } g.DrawString(checkCode.Substring(i, </span><span style="color: rgb(128,0,128)">1</span><span style="color: rgb(0,0,0)">), f, b, </span><span style="color: rgb(128,0,128)">3</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,0)">+</span><span style="color: rgb(0,0,0)"> (i </span><span style="color: rgb(0,0,0)">*</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,128)">12</span><span style="color: rgb(0,0,0)">), ii); } </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">画一个边框</span><span style="color: rgb(0,128,0)"></span><span style="color: rgb(0,0,0)"> g.DrawRectangle(</span><span style="color: rgb(0,0,255)">new</span><span style="color: rgb(0,0,0)"> Pen(Color.Black, </span><span style="color: rgb(128,0,128)">0</span><span style="color: rgb(0,0,0)">), </span><span style="color: rgb(128,0,128)">0</span><span style="color: rgb(0,0,0)">, </span><span style="color: rgb(128,0,128)">0</span><span style="color: rgb(0,0,0)">, image.Width </span><span style="color: rgb(0,0,0)">-</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,128)">1</span><span style="color: rgb(0,0,0)">, image.Height </span><span style="color: rgb(0,0,0)">-</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,128)">1</span><span style="color: rgb(0,0,0)">); </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">输出到浏览器</span><span style="color: rgb(0,128,0)"></span><span style="color: rgb(0,0,0)"> System.IO.MemoryStream ms </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">new</span><span style="color: rgb(0,0,0)"> System.IO.MemoryStream(); image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); HttpContext.Current.Response.ClearContent(); </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">Response.ClearContent();</span><span style="color: rgb(0,128,0)"></span><span style="color: rgb(0,0,0)"> HttpContext.Current.Response.ContentType </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">image/Jpeg</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(0,0,0)">; HttpContext.Current.Response.BinaryWrite(ms.ToArray()); g.Dispose(); image.Dispose(); }</span></div></pre> </div> </span></div>
试试其它关键字
///
///
生成随机的字母
///
同语言下
.
gzip压缩
.
实现http多线程断点续传下载文件
.
实现多线程断点续传下载大文件
.
生成字符串的 CheckSum
.
根据 UserAgent 获取浏览器的类型和版本
.
根据 Agent 判断是否是智能手机
.
隐藏手机号中间四位为*方法
.
合并图片(二维码和其他图片合并)
.
ASP.NET CORE中判断是否移动端打开网页
.
ASP.NET(C#)实现页面计时(定时)自动跳转
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
Dezai.CN
贡献的其它代码
(
4037
)
.
多线程Socket服务器模块
.
生成随机密码
.
清除浮动样式
.
弹出窗口居中
.
抓取url的函数
.
使用base HTTP验证
.
div模拟iframe嵌入效果
.
通过header转向的方法
.
Session操作类
.
执行sqlite输入插入操作后获得自动编号的ID
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3