A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 谢华良 中级黑马   /  2012-10-26 20:56  /  2321 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

刚开始为登录页添加验证码功能时,把验证码代码写在了登录页的后台代码,运行起来只有验证码显示,别的没有了。才知道验证码是要另外写在一个页,调用就可以了。
登录页就那点功能,别的代码也不写了,就写个验证码的吧。
Login.aspx页的代码
[csharp] view plaincopy

  • <span style="font-size:18px;"> <label style="text-align:right" >验证码</label>  
  •   <asp:TextBox style="width:50px;float:left;margin-left:18px"></asp:TextBox>  
  • <input class="text-input" name="CheckCode" type="text"   
  • style="width:50px;float:left;margin-left:18px" />  
  •   <img src="CheckCode.aspx" style="margin-left:18px" alt="点击刷新验证码"  
  • onclick="this.src='CheckCode.aspx?rnd='+Math.random();" /></span>  


login.aspx.cs页的代码就不写了,主要就是登录的逻辑代码。




下面是验证码页的代码
CheckCode.aspx页没有代码。需要一张透明的图片,用来显示验证码。
CheckCode.aspx.cs代码
[csharp] view plaincopy

  • <span style="font-size:18px;">using System;  
  • using System.Collections.Generic;  
  • using System.Linq;  
  • using System.Web;  
  • using System.Web.UI;  
  • using System.Web.UI.WebControls;  
  • using System.Drawing;  
  • using System.Drawing.Drawing2D;  
  • using System.Drawing.Imaging;  
  • using System.IO;  
  • using System.Collections;  
  •   
  • public partial class System_CheckCode : System.Web.UI.Page  
  • {  
  •     protected void Page_Load(object sender, EventArgs e)  
  •     {  
  •         //把生成的验证码字符串赋值给code  
  •         string code = GenerateCheckCode();  
  •         //用Cookie保存验证码,一分钟过期  
  •         HttpCookie CheckCode = new HttpCookie("CheckCode", code);  
  •         CheckCode.Expires = new DateTime(6000);  
  •         Response.AppendCookie(CheckCode);  
  •         //根据生成的验证码绘制图片  
  •         CreatCheckCodeImage(CheckCode.Value);  
  •     }  
  •     //生成验证码字符串  
  •     private string GenerateCheckCode()  
  •     {  
  •         int number;  
  •         char code;  
  •         string checkCode = string.Empty;  
  •         Random random = new Random();  
  •         //可设置验证码字符数量  
  •         for (int i = 0; i < 4; i++)  
  •         {  
  •             //返回非负随机数  
  •             number = random.Next();  
  •   
  •             //为生成随机数设置条件  
  •             if (number % 2 == 0)  
  •             {  
  •                 code = (char)('0' + (char)(number % 10));  
  •             }  
  •             else  
  •             {  
  •                 code = (char)('A' + (char)(number % 26));  
  •             }  
  •             //完整验证码  
  •             checkCode += code.ToString();  
  •         }  
  •         return checkCode;  
  •   
  •     }  
  •   
  •   
  •     //根据传入的验证码字符串生成图片  
  •     private void CreatCheckCodeImage(string checkCode)  
  •     {  
  •         if (checkCode == null || checkCode.Trim() == string.Empty) return;  
  •         Bitmap image = new Bitmap((int)(Math.Ceiling(checkCode.Length * 17.5)), 28);  
  •   
  •         Graphics g = Graphics.FromImage(image);  
  •         try  
  •         {  
  •             Random random = new Random();  
  •             //清空图片背景色  
  •             g.Clear(Color.White);  
  •             //画图片的背景噪音线  
  •             for (int i = 0; i < 25; i++)  
  •             {  
  •                 int x1 = random.Next(image.Width);  
  •                 int x2 = random.Next(image.Width);  
  •                 int y1 = random.Next(image.Width);  
  •                 int y2 = random.Next(image.Width);  
  •                 //绘制一条由坐标对指定的两个点的线条  
  •                 g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);  
  •   
  •             }  
  •   
  •             //设置字体样式  使用指定的大小和样式初始化新Font  
  •             Font font = new Font("Arial", 17, (FontStyle.Bold | FontStyle.Italic));  
  •   
  •             //使用线性渐变封装 Brush。此类不能被继承。  
  •             //Rectangle(Int32, Int32, Int32, Int32) 用指定的位置和大小初始化 Rectangle 类的新实例  
  •             LinearGradientBrush brush =  
  • new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true);  
  •             //DrawString(String, Font, Brush, Single, Single)     
  • <span style="font-size:18px;">//</span>在指定位置并且用指定的 Brush 和 Font 对象绘制指定的文本字符串。  
  •             g.DrawString(checkCode, font, brush, 2, 2);  
  •   
  •             //画图片的前景噪音点  
  •             for (int i = 0; i < 100; i++)  
  •             {  
  •                 int x = random.Next(image.Width);  
  •                 int y = random.Next(image.Height);  
  •                 //image.SetPixel(x,y,Color)  获取此 Bitmap 中指定像素的颜色。  
  •                 //  FromArgb(Int32) 从一个 32 位 ARGB 值创建 Color 结构。  
  •                 image.SetPixel(x, y, Color.FromArgb(random.Next()));  
  •             }  
  •   
  •             //画图片的边框线  
  •             g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);  
  •             MemoryStream ms = new MemoryStream();  
  •             image.Save(ms, ImageFormat.Gif);  
  •             Response.ClearContent();  
  •             Response.ContentType = "image/Gif";  
  •             Response.BinaryWrite(ms.ToArray());  
  •   
  •         }  
  •         finally  
  •         {  
  •             g.Dispose();  
  •             image.Dispose();  
  •         }  
  •   
  •     }  
  • }</span>  



评分

参与人数 1技术分 +2 收起 理由
邓艳秋 + 2

查看全部评分

2 个回复

倒序浏览
好帖子要好好学习,顶上去~~
回复 使用道具 举报
网页刷新会自动走所有代码 写在本页里面还没有输入验证码 进入网页不处理肯定会出现问题

评分

参与人数 1技术分 +1 收起 理由
邓艳秋 + 1

查看全部评分

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马