易截截图软件、单文件、免安装、纯绿色、仅160KB

Asp.net(C#)实现验证码功能代码

新建一个专门用来创建验证码图片的页面ValidateCode.aspx
它的后台cs文件代码如下:
PageLoad
复制代码 代码如下:
private void Page_Load(object sender, System.EventArgs e)
{
string checkCode = CreateRandomCode(4);
Session["CheckCode"] = checkCode;
CreateImage(checkCode);
}
其中CreateRandomCode是自定义的函数,参数代表验证码位数
复制代码 代码如下:
private string CreateRandomCode(int codeCount)
{
string allChar = "0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,W,X,Y,Z";
string[] allCharArray = allChar.Split(',');
string randomCode = "";
int temp = -1; Random rand = new Random();
for (int i = 0; i < codeCount; i++)
{
if (temp != -1)
{
rand = new Random(i * temp * ((int)DateTime.Now.Ticks));
}
int t = rand.Next(35);
if (temp == t)
{
return CreateRandomCode(codeCount);
}
temp = t;
randomCode += allCharArray[t];
}
return randomCode;
}
CreateImage也是一个自定义的函数,用于生成图
复制代码 代码如下:
private void CreateImage(string checkCode)
{
int iwidth = (int)(checkCode.Length * 11.5);
System.Drawing.Bitmap image = new System.Drawing.Bitmap(iwidth, 20);
Graphics g = Graphics.fromImage(image);
Font f = new System.Drawing.Font("Arial", 10, System.Drawing.FontStyle.Bold);
Brush b = new System.Drawing.SolidBrush(Color.White);
//g.FillRectangle(new System.Drawing.SolidBrush(Color.Blue),0,0,image.Width, image.Height);
g.Clear(Color.Blue);
g.DrawString(checkCode, f, b, 3, 3);
Pen blackPen = new Pen(Color.Black, 0);
Random rand = new Random();
for (int i=0;i<5;i++)
{
int y = rand.Next(image.Height);
g.DrawLine(blackPen,0,y,image.Width,y);
}
System.IO.MemoryStream ms = new System.IO.MemoryStream();
image.Save(ms,System.Drawing.Imaging.ImageFormat.Jpeg);
Response.ClearContent();
Response.ContentType = "image/Jpeg";
Response.BinaryWrite(ms.ToArray());
g.Dispose();
image.Dispose();
}
//g.FillRectangle(new System.Drawing.SolidBrush(Color.Blue),0,0,image.Wi


相关文档:

Windows 2003中配置ASP.Net环境

大家知道,Microsoft为了更好地预防恶意用户和攻击者的攻击,在默认情况下,没有将 IIS6.0 安装到 Windows Server 2003 家族的成员上。而且,当我们最初安装 IIS6.0 时,该服务在高度安全和"锁定"模式下安装。在默认情况下,IIS6.0 只为静态内容提供服务即,诸如 ASP、ASP.NET、在服务器端的包含文件、WebDAV 发布和 FrontP ......

JSON Serialization and Deserialization in ASP.Net


    
JSON Serialization and Deserialization in ASP.Net
 
I was looking around for a simple example which would just do an object serialization to a JSON format, and then deserializing back to the original object. I found few examples on MSDN, but did seem to be too long ......

ASP.Net全局变量的设置和读取方法

本文介绍两种ASP.Net项目中全局变量使用的方式。web.config文件 和 Gloab文件。以下分别说明:
方法一:web.config文件
——设置:
在web.config文件里添加关键字key是通过<appSettings>标记来实现的,但是appSettings标记通常放在<system.web>.....</system.web>标记外面。例:
<conf ......

将jQuery应用于Asp.net Ajax时,应避免的3个错误

3 mistakes to avoid when using jQuery with ASP.NET AJAX
AJAX, ASP.NET, JavaScript, jQuery By Dave Ward on June 5th, 2008
Over the past few weeks, I think I have definitely embodied Jeff Atwood’s claim that we’re all amateurs, learning together. Despite my best efforts to thoroughly tes ......

【转】asp.net 页面刷新方法总结

ASP.NET页面刷新方法总结
先看看ASP.NET页面刷新的实现方法:
第一:
private void Button1_Click(objectsender,System.EventArgse)
{
Response.Redirect(Request.Url.ToString());
}
第二:
private void Button2_Click(objectsender,System.EventArgse)
{
Response.Write("<mce:script language=javascript& ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号