惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

N
News | PayPal Newsroom
Security Archives - TechRepublic
Security Archives - TechRepublic
Hacker News: Ask HN
Hacker News: Ask HN
H
Hacker News: Front Page
Apple Machine Learning Research
Apple Machine Learning Research
TaoSecurity Blog
TaoSecurity Blog
Help Net Security
Help Net Security
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
V
V2EX
Hugging Face - Blog
Hugging Face - Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
人人都是产品经理
人人都是产品经理
博客园 - 三生石上(FineUI控件)
Security Latest
Security Latest
Cloudbric
Cloudbric
WordPress大学
WordPress大学
S
SegmentFault 最新的问题
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Know Your Adversary
Know Your Adversary
A
Arctic Wolf
L
LangChain Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
The GitHub Blog
The GitHub Blog
P
Proofpoint News Feed
W
WeLiveSecurity
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
M
MIT News - Artificial intelligence
Google DeepMind News
Google DeepMind News
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
The Cloudflare Blog
小众软件
小众软件
NISL@THU
NISL@THU
云风的 BLOG
云风的 BLOG
P
Privacy & Cybersecurity Law Blog
S
Security @ Cisco Blogs
博客园 - 【当耐特】
I
InfoQ
Vercel News
Vercel News
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
P
Proofpoint News Feed
O
OpenAI News
Google DeepMind News
Google DeepMind News
N
News and Events Feed by Topic
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
K
Kaspersky official blog
T
Threat Research - Cisco Blogs
量子位
宝玉的分享
宝玉的分享

博客园 - goodbaby

辞职了 爱上无聊 读程序有感 把网通的网关接口程序重写了 深深陷入困境 我的技术和观点 我的AOP初步 静态和动态控件回递数据的处理差别 membership and roleship 我的asp.net 2.0初体验 一点技术,一点生活 简单体验多层应用 基于服务的架构 变化,感触 又可以进自己的blog了 浅谈asp.net UI 浅谈基于角色的安全 浅谈数据库里的自引用 简单的URL重写
浅谈验证码
goodbaby · 2004-11-30 · via 博客园 - goodbaby

参考了一些blog和一些新闻组,我选取了一种简单可行的方案,就是用Session来保存验证码。我进行了简单的实现。
下面是一些代码片段:

private void btnTest_Click(object sender, System.EventArgs e)
  {
   if(int.Parse(txtCode.Text)==(int)Session["Code"])
      Response.Redirect("AnotherPage.aspx");//用于测试页
   lblStatus.Text="sorry Authentication is not match";
   BuildRandom();
  }

用于生成随机数。
  private void BuildRandom()
  {
   Random random=new Random();
   int AuthenticationCode=random.Next(1000,9999);//产生四位随机数
   Session["Code"]=AuthenticationCode;//save in server for authenticate
   Code.Src="Code.aspx";
  }
最后是一个绘图函数:

 Response.ContentType="image/gif";
   string text=((int)Session["Code"]).ToString();
            Bitmap CodePic=new Bitmap(50,30);
   Graphics oGraphics=Graphics.FromImage(CodePic);
   DrawText(oGraphics,0,0,50,30,text);
   Response.ClearContent();
   CodePic.Save(Response.OutputStream,System.Drawing.Imaging.ImageFormat.Gif);
            oGraphics.Dispose();
   CodePic.Dispose();
  }

  private void DrawText(Graphics oGraphics,int iTop,int iLeft,int iWidth,int iHeight,string sText)
  {
   SolidBrush oBrushForBack=new SolidBrush(Color.DeepPink);
   oGraphics.FillRectangle(oBrushForBack,0,0,iWidth,iHeight);//将位图刷白
   FontStyle eFontStyle=FontStyle.Regular;//字体样式
   int iFontSize=8;
   string sColor="Black";
   StringAlignment eAlign=StringAlignment.Near;
   StringFormatFlags eFlag=StringFormatFlags.DirectionVertical;
   Rectangle oRect=new Rectangle(iTop,iLeft,iWidth,iHeight);
   Font oFont=new Font("Arial",iFontSize,eFontStyle);
   StringFormat oFormat=new StringFormat();//字符串格式
   oFormat.Alignment=eAlign;//设置字符串格式
   oFormat.LineAlignment=StringAlignment.Center;//设置字符串格式
   SolidBrush oBrush=new SolidBrush(Color.FromName(sColor));
   oGraphics.DrawString(sText,oFont,oBrush,oRect,oFormat);
   oBrush.Dispose();
   oBrushForBack.Dispose();
  }

很简单。在浏览器里执行后,第一次很慢,但后来载入图片就很快了。