急求asp.net的验证码程序

fluxayxxx 2005-03-16 08:57:52
哪位大哥大姐有的请回复~~~~
...全文
175 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
waki 2005-05-05
  • 打赏
  • 举报
回复
使用我新做的验证码控件就行了,免去好多麻烦。
WakiCheckDigit for asp.net 是适合于微软.Net开发平台的Web控件。它能放进Web 工具箱中,并可拖入 WEB 页面进行可视化的设置。它可以方便快速生成验证码,用于用户登录,身份验证等。可防止程序穷举破解用户信息。不用编程,一分钟生成安全验证系统,为你的系统构筑多一道安全防线。
下载地址:
http://www.aspxcontrol.com/download/wakicheckdigit.rar
Ivony 2005-03-17
  • 打赏
  • 举报
回复
public static void ValidateCode( Font font, Color foreColor, Color backColor, Stream stream )
{
ValidateCode( MakeValidateCode(4), font, foreColor, backColor, stream );
}

public static void ValidateCode( string str, Font font, Color foreColor, Color backColor, Stream stream)
{
Bitmap paper = new Bitmap( (int) font.SizeInPoints * System.Text.Encoding.GetEncoding("gb2312").GetByteCount( str ), font.Height, PixelFormat.Format24bppRgb );//创建画布

#region 作图

Graphics g = Graphics.FromImage( paper );


//填充背景
g.FillRectangle( new SolidBrush( backColor ), 0,0, paper.Width, paper.Height );
//画出字符
g.DrawString( str, font, new SolidBrush( foreColor ), new PointF( 0, 0 ) );
//添加干扰线
g.DrawBezier( new Pen( new SolidBrush( foreColor ), 1 ), GetRandomPoint(0, paper.Height), new Point( paper.Width ,paper.Height), GetRandomPoint(paper.Width, paper.Height), GetRandomPoint(paper.Width, paper.Height) );
//完成保存
g.Save();

#endregion

paper.Save( stream, ImageFormat.Png );
}

private static Point GetRandomPoint ( int x, int y )
{
return new Point( _rand.Next( x ), _rand.Next( y ) );
}

public static string MakeValidateCode(int length)
{
// char[] s = new char[]{'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','v','w','x','y','z'};
char[] s = @"1234567890".ToCharArray();
string num = "";
Random r = new Random();
for(int i = 0; i < length; i++)
{
num += s[r.Next(0, s.Length)].ToString();
}
return num;
}


//使用:
private void Page_Load(object sender, System.EventArgs e)
{

string code;

if ( Session["ValidateCode"] == null )
code = "名智久久";
else
code = Session["ValidateCode"].ToString();

Response.Cache.SetCacheability( HttpCacheability.NoCache );

Response.ContentType = "image/png";
System.IO.MemoryStream ms = new System.IO.MemoryStream();
WebUtility.ValidateCode( code, new Font("Courier New", 18, FontStyle.Bold), Color.Black, Color.White, ms);
ms.WriteTo( Response.OutputStream );
}
fphuang 2005-03-17
  • 打赏
  • 举报
回复
http://community.csdn.net/Expert/topic/3848/3848091.xml?temp=.12344
nga96 2005-03-17
  • 打赏
  • 举报
回复
UP
syeerzy 2005-03-17
  • 打赏
  • 举报
回复
验证码?是进CSDN的时候,密码下面那个?

偶只有C#的。。。。。
fluxayxxx 2005-03-17
  • 打赏
  • 举报
回复
c#我不太会,有没有vb的
wangyu2481 2005-03-17
  • 打赏
  • 举报
回复
学习!
luyiping 2005-03-17
  • 打赏
  • 举报
回复
mark
ldljlq 2005-03-16
  • 打赏
  • 举报
回复
新建一个专门用来创建验证码图片的页面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.Width, image.Height);
g.Clear(Color.Blue);
这两种方法都可以改变生成图片的背景颜色
下面那个for循环用来生成一些随机的水平线

在需要用到验证码的页面添加一个<asp:Image>控件即可,但是要把ImageUrl指向生成验证码的页面

<asp:Image Runat="server" ID="ImageCheck" ImageUrl="ValidateCode.aspx"></asp:Image>

PS:这是网上转的。
hedonister 2005-03-16
  • 打赏
  • 举报
回复
什么的验证码?

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧