ASP.NET 验证码控件
public class ValidateCode : WebControl
{
/// <summary>
/// 默认构造函数,暴露的属性接口
/// </summary>
public ValidateCode()
{
this.CharCount = ;
this.Width = ;
this.Height = ;
CodeColor = Color.White;
} /// <summary>
/// 设置参数
/// </summary>
/// <param name="CharCount"></param>
/// <param name="Width"></param>
/// <param name="Height"></param>
/// <param name="color"></param>
public ValidateCode(int CharCount, int Width, int Height,Color color)
{
this.CharCount = CharCount;
this.Width = Width;
this.Height = Height;
this.CodeColor = color;
}
/// <summary>
/// 字符个数
/// </summary>
public int CharCount
{
get;
set;
}
/// <summary>
/// 图标矩形宽度
/// </summary>
public new int Width
{
set;
get;
}
/// <summary>
/// 图标矩形高度
/// </summary>
public new int Height
{
set;
get;
} /// <summary>
/// Code背景色
/// </summary>
public Color CodeColor
{
get;
set;
} /// <summary>
/// 验证验证码是否正确
/// </summary>
/// <param name="sn"></param>
/// <returns></returns>
public bool checkCode(string sn)
{
return (sn.ToUpper() == this.Page.Request.Cookies["validateCookie"].Values["ChkCode"].ToString().ToUpper());//读取Cookie
}
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
if (this.DesignMode)
return;
string str = this.Page.Request.QueryString["_xuImageTag"]; if (str != "")
return;
HttpResponse resp = this.Page.Response;
string chkCode = string.Empty; //颜色列表,用于验证码、噪线、噪点
Color[] color = { Color.Red, Color.Black, Color.Blue, Color.Green, Color.Orange }; string[] font = { "宋体", "Times New Roman", "MS Mincho", "楷体", "隶书", "微软雅黑","Calibri" };
char[] character ={'','','','','','','','','','','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'};
Random rnd = new Random();
for (int i = ; i < this.CharCount; i++)
{
chkCode += character[rnd.Next(character.Length)];
} resp.Cookies["validateCookie"].Values["ChkCode"] =chkCode;//验证码写入Cookie Bitmap bmp = new Bitmap(this.Width,this.Height);
Graphics g = Graphics.FromImage(bmp);
g.Clear(this.CodeColor); //画噪线
for (int i = ; i < ; i++)
{
int x1 = rnd.Next(this.Width);
int y1 = rnd.Next(this.Height);
int x2 = rnd.Next(this.Width);
int y2 = rnd.Next(this.Height);
Color clr = color[rnd.Next(color.Length)];
g.DrawLine(new Pen(clr), x1, y1, x2, y2);
}
//画验证字符串
for (int i = ; i < chkCode.Length; i++)
{
string fnt = font[rnd.Next(font.Length)];
Font ft = new Font(fnt, );
Color clr = color[rnd.Next(color.Length)];
g.DrawString(chkCode[i].ToString(), ft, new SolidBrush(clr), (float)i * this.Width/chkCode.Length++, ((float)this.Height)/);
} for (int i = ; i < (this.Width*this.Height)/; i++)
{
int x = rnd.Next(bmp.Width);
int y = rnd.Next(bmp.Height);
Color clr = color[rnd.Next(color.Length)];
bmp.SetPixel(x, y, clr);
} //将验证图片写入内存流,并将以image/Png格式输出
MemoryStream ms = new MemoryStream();
try
{
bmp.Save(ms, ImageFormat.Png);
resp.ClearContent();
resp.ContentType = "image/Png";
resp.BinaryWrite(ms.ToArray());
resp.Flush();
resp.End();
}
finally
{
bmp.Dispose();
g.Dispose();
} }
protected override void Render(HtmlTextWriter writer)
{ if (!this.DesignMode)
{
writer.Write("<img border=\"0\" src=\"{0}\">", this.Page.Request.Path + "?_xuImageTag=1");
}
else
{
writer.Write("验证码");
}
base.Render(writer);
}
}
ASP.NET 验证码控件的更多相关文章
- asp.net分页控件
一.说明 AspNetPager.dll这个分页控件主要用于asp.net webform网站,现将整理代码如下 二.代码 1.首先在测试页面Default.aspx页面添加引用 <%@ Reg ...
- asp.net ajax控件tab扩展,极品啊,秒杀其它插件
说明:asp.net ajax控件tab要设置width和height,而且在线文本编辑器放能够放入tab中,也必须是asp.net的控件型在线文本,例如fckeditor,下面是我设置好的配置. & ...
- javascript获取asp.net服务器端控件的值
代码如下: <%@ Page Language="C#" CodeFile="A.aspx.cs" Inherits="OrderManage_ ...
- ASP.NET控件<ASP:Button /> html控件<input type="button">区别联系
ASP.NET控件<ASP:Button />-------html控件<input type="button">杨中科是这么说的:asp和input是一样 ...
- ux.form.field.Verify 验证码控件
//验证码控件 Ext.define('ux.form.field.Verify', { extend: 'Ext.container.Container', alias: ['widget.fiel ...
- [ASP.NET]asp.net Repeater控件的使用方法
asp.net Repeater控件的使用方法 -- : 4770人阅读 评论() 收藏 举报 asp.netserveraspdatasetdeletexhtml 今天学习了,Repeater控件 ...
- 关于ASP.net TextBox控件的失去焦点后触发其它事件
编写人:CC阿爸 2015-2-02 今天在这里,我想与大家一起分享如何处理的ASP.net TextBox控件的失去焦点后触发其它事件的问题,在此做个小结,以供参考.有兴趣的同学,可以一同探讨与学习 ...
- 浅谈ASP.NET报表控件
OWC似乎使用者居多,但看见有网友在帖中抱怨OWC在使用时需要许可证书,于是将其排除,我可不想BOSS在看报表时弹出一个“没有许可证书”的窗口. 接着找到了ComponentOne的Web chart ...
- 要后台控制前台的的CSS样式,我们可以加入ASP.NET Literal 控件
ASP.NET Literal 控件,用于在页面上显示文本.此文本是可编程的. 我用它来制作了 ) { this.LtdMemberPromotion7.Text = "<style ...
随机推荐
- ASP.NET中将导出的数据以UTF-8编码方式进行存储
Response.Charset = "UTF-8"; Response.ContentEncoding = Encoding.UTF8; Response.AppendHea ...
- linux基础快速掌握课件
分享一个很好的linux课件,可以快速掌握linux的基础.猛击下面的链接地址打开 http://pan.baidu.com/s/14oa9w
- [No00008B]远程桌面发送“Ctrl+Alt+Delete”组合键调用任务管理器
向远程桌面发送"Ctrl+Alt+Delete"组合键的两种方法 1.在本地按下Ctrl+Alt+End,可以成功发送"Ctrl+Alt+Delete"组合键! ...
- mysql max_allowed_packet 设置过小导致记录写入失败
mysql根据配置文件会限制server接受的数据包大小. 有时候大的插入和更新会受max_allowed_packet 参数限制,导致写入或者更新失败. 查看目前配置 show VARIABLES ...
- LTE中的各种ID含义
原文链接:http://www.hropt.com/ask/?q-7128.html ECI (28 Bits) = eNB ID(20 Bits) + Cell ID(8 Bits) 换成16进制就 ...
- C# readonly 与const
引自:http://www.cnblogs.com/ryuasuka/p/3342282.html?utm_source=tuicool&utm_medium=referral 现在正在学&l ...
- com.panie 项目开发随笔_功能任务设计(2016.12.28)
(一) 第一个菜单 做什么好呢? 1)上次 在研究的功能 是 爬虫,需要将定时爬虫的任务加进来 2)博客的页面,也需要重新布局出来 3)需要做一个,添加博客的页面 (二) 那就先做博客管理吧! 先添加 ...
- xv6的课本翻译之——附录B 系统启动器
Appendix B 附录 B Figure B-1 The relationship between logical, linear, and physical addresses. 图B-1:逻辑 ...
- 【BZOJ-4380】Myjnie 区间DP
4380: [POI2015]Myjnie Time Limit: 40 Sec Memory Limit: 256 MBSec Special JudgeSubmit: 162 Solved: ...
- CVE-2016-1240 Tomcat 服务本地提权漏洞
catalogue . 漏洞背景 . 影响范围 . 漏洞原理 . 漏洞PoC . 修复方案 1. 漏洞背景 Tomcat是个运行在Apache上的应用服务器,支持运行Servlet/JSP应用程序的容 ...