用C#编写验证码的方法

这几天看了一些网友关于C#编写验证码的问题,特地关心了一下,自己整理了一些供大家参考学习!

一、验证码的C#源码如下(文件VerifyCode.aspx):

None.gif using  System;
None.gif
using  System.Data;
None.gif
using  System.Configuration;
None.gif
using  System.Collections;
None.gif
using  System.Web;
None.gif
using  System.Web.Security;
None.gif
using  System.Web.UI;
None.gif
using  System.Web.UI.WebControls;
None.gif
using  System.Web.UI.HtmlControls;
None.gif
using  System.Drawing;
None.gif
using  System.Drawing.Drawing2D;
None.gif
None.gif
namespace  validateCode
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// WebForm2 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class WebForm2 : System.Web.UI.Page
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
protected System.Web.UI.WebControls.Image Image1;
InBlock.gif    
InBlock.gif        
private void Page_Load(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.CreateImage(GenerateCheckCode());
ExpandedSubBlockEnd.gif        }

InBlock.gif        
private string GenerateCheckCode()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
int number;
InBlock.gif            
char code;
InBlock.gif            
string checkCode = String.Empty;
InBlock.gif
InBlock.gif            System.Random random 
= new Random();
InBlock.gif            
for (int i = 0; i < 4; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                number 
= random.Next();
InBlock.gif                code 
= (char)('0' + (char)(number % 10));
InBlock.gif                
if (number % 2 == 0)
InBlock.gif                    code 
= (char)('0' + (char)(number % 10));
InBlock.gif                
else
InBlock.gif                    code 
= (char)('A' + (char)(number % 26));//数字加英文字
InBlock.gif
                checkCode += code.ToString();
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            Response.Cookies.Add(
new HttpCookie("myCheckCode", checkCode));
InBlock.gif
InBlock.gif            
return checkCode;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
private void CreateImage(string checkCode)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
int iwidth = (int)(checkCode.Length * 13);
InBlock.gif            System.Drawing.Bitmap image 
= new System.Drawing.Bitmap(iwidth, 25);            
InBlock.gif            Graphics g 
= Graphics.FromImage(image);
InBlock.gif            g.Clear(Color.White);
InBlock.gif            
//定义颜色
ExpandedSubBlockStart.gifContractedSubBlock.gif
            Color[] c = dot.gif{ Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Orange, Color.Brown, Color.DarkCyan, Color.Purple };
InBlock.gif            
//定义字体
ExpandedSubBlockStart.gifContractedSubBlock.gif
            string[] font = dot.gif"Verdana""Microsoft Sans Serif""Comic Sans MS""Arial""宋体" };
InBlock.gif            Random rand 
= new Random();
InBlock.gif            
//随机输出噪点
InBlock.gif
            for (int i = 0; i < 2; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
int x = rand.Next(image.Width);
InBlock.gif                
int y = rand.Next(image.Height);
InBlock.gif                g.DrawRectangle(
new Pen(Color.LightGray, 0), x, y, 11);
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
//输出不同字体和颜色的验证码字符
InBlock.gif
            for (int i = 0; i < checkCode.Length; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
int cindex = rand.Next(7);
InBlock.gif                
int findex = rand.Next(5);
InBlock.gif
InBlock.gif                Font f 
= new System.Drawing.Font(font[findex], 10, System.Drawing.FontStyle.Bold);
InBlock.gif
InBlock.gif                Brush b 
= new System.Drawing.SolidBrush(c[cindex]);
InBlock.gif                
int ii = 4;
InBlock.gif                
if ((i + 1% 2 == 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    ii 
= 2;
ExpandedSubBlockEnd.gif                }

InBlock.gif                g.DrawString(checkCode.Substring(i, 
1), f, b, 3 + (i * 12), ii);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
//画一个边框
InBlock.gif
            g.DrawRectangle(new Pen(Color.Black,0), 00, image.Width - 2, image.Height - 2);
InBlock.gif            
//输出到浏览器
InBlock.gif
            System.IO.MemoryStream ms = new System.IO.MemoryStream();            
InBlock.gif
//            image.Save("pic",System.Drawing.Imaging.ImageFormat.Jpeg);
InBlock.gif
            image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
InBlock.gif            Response.ClearContent();
InBlock.gif            Response.ContentType 
= "image/Jpeg";
InBlock.gif            Response.BinaryWrite(ms.ToArray());
InBlock.gif
//            this.Image1.ImageUrl="pic.jpg";
InBlock.gif
            g.Dispose();
InBlock.gif            image.Dispose();
ExpandedSubBlockEnd.gif        }

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
InBlock.gif        
override protected void OnInit(EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//
InBlock.gif            
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
InBlock.gif            
//
InBlock.gif
            InitializeComponent();
InBlock.gif            
base.OnInit(e);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
InBlock.gif        
/// 此方法的内容。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private void InitializeComponent()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{    
InBlock.gif            
this.Load += new System.EventHandler(this.Page_Load);
InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

二、C#验证码的使用方法:
在其他需要用到验证码的.aspx的html页里添加如下标签代码即可!
<img src="VerifyCode.aspx">
三、只刷新验证码的实现方法:
在上面的.aspx的html页添加如下代码:
<input class="buttonstyle" style="WIDTH: 120px" οnclick="document.getElementById('imcheckingcode').src='VerifyCode.aspx?temp='+Math.random();" type="button" value="Change Code" />
四、在登录页面的登录按钮的处理事件中使用以下代码判断验证码:

None.gif private   void  btnLogin_Click( object  sender, System.Web.UI.ImageClickEventArgs e)
ExpandedBlockStart.gifContractedBlock.gif  
dot.gif {
InBlock.gif   
if(Request.Cookies["CheckCode"== null)
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    lblMessage.Text 
= "您的浏览器设置已被禁用 Cookies,您必须设置浏览器允许使用 Cookies 选项后才能使用本系统。";
InBlock.gif    lblMessage.Visible 
= true;
InBlock.gif    
return;
ExpandedSubBlockEnd.gif   }
 
InBlock.gif
InBlock.gif   
if(String.Compare(Request.Cookies["CheckCode"].Value, txtCheckCode.Text, true!= 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    lblMessage.Text 
= "验证码错误,请输入正确的验证码。";
InBlock.gif    lblMessage.Visible 
= true;
InBlock.gif    
return;
ExpandedSubBlockEnd.gif   }

以上整理的关于C#验证码资料,供大家参考,多提意见!!!

转载于:https://www.cnblogs.com/mc-dragon/archive/2007/01/12/ValidateCode.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值