在做一个登录页时,我们通常需要设置验证码,防止非法人员通过其他手段非法登录。在做一个页面的验证码时,我们需要先建立一个Active的Web页面,来随机的产生数字和字母,同时把产生的数字存储在Session中,传递给登录页,用来验证。并且定义一个画板,在Active页面绘出验证码的背景颜色,在Active页面的代码如下:(textBox3,imagbox1)
[csharp] view plaincopyprint?
01.protected void Page_Load(object sender, EventArgs e)
02. {
03. //显示验证码
04. this.GenImg(this.GenCode(4));
05. Session["image"] = this.GenCode(4);
06. Server.Transfer("Default.aspx");
07. //Console.WriteLine(this.GenCode(4));
08. }
09. private string GenCode(int num)
10. {
11. string [] source={"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"};
12. string code = "";
13. //创建Random类的实例
14. Random rd = new Random();
15. //获取验证码
16. for (int i = 0; i < num; i++)
17. {
18. code += source[rd.Next(0, source.Length)];
19.
20. }
21. //返回产生的验证码
22. return code;
23. }
24.
25. //生成图片
26. private void GenImg(string code)
本内容试读结束,登录后可阅读更多
下载后可阅读完整内容,剩余3页未读,立即下载