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

推荐订阅源

P
Proofpoint News Feed
博客园 - 聂微东
Application and Cybersecurity Blog
Application and Cybersecurity Blog
MyScale Blog
MyScale Blog
罗磊的独立博客
H
Help Net Security
L
LangChain Blog
T
Threat Research - Cisco Blogs
量子位
S
Securelist
Last Week in AI
Last Week in AI
L
Lohrmann on Cybersecurity
T
The Exploit Database - CXSecurity.com
P
Privacy International News Feed
The Hacker News
The Hacker News
Vercel News
Vercel News
D
Darknet – Hacking Tools, Hacker News & Cyber Security
C
Cybersecurity and Infrastructure Security Agency CISA
T
The Blog of Author Tim Ferriss
T
Threatpost
Security Latest
Security Latest
P
Palo Alto Networks Blog
Microsoft Security Blog
Microsoft Security Blog
NISL@THU
NISL@THU
F
Full Disclosure
WordPress大学
WordPress大学
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Stack Overflow Blog
Stack Overflow Blog
C
Check Point Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
酷 壳 – CoolShell
酷 壳 – CoolShell
H
Heimdal Security Blog
J
Java Code Geeks
Recorded Future
Recorded Future
Hugging Face - Blog
Hugging Face - Blog
G
GRAHAM CLULEY
Know Your Adversary
Know Your Adversary
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
阮一峰的网络日志
阮一峰的网络日志
U
Unit 42
B
Blog RSS Feed
月光博客
月光博客
C
Cisco Blogs
V
Visual Studio Blog
D
DataBreaches.Net
H
Hacker News: Front Page
博客园 - 叶小钗
N
News and Events Feed by Topic
爱范儿
爱范儿
A
Arctic Wolf

博客园 - 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();
  }

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