标题:asp.net的网页中,怎么作随机验证码?
只看楼主
ping16002
Rank: 1
等 级:新手上路
帖 子:78
专家分:0
注 册:2007-11-2
 问题点数:0 回复次数:6 
asp.net的网页中,怎么作随机验证码?
的网页中,怎么作随机验证码?
就像网页的登录中的那样,有个随机的验证码。怎么来做 这个验证码 ?
搜索更多相关主题的帖子: 网页 asp 随机 验证 
2008-03-24 08:57
寂寞的刺猬
Rank: 1
等 级:新手上路
帖 子:111
专家分:0
注 册:2008-3-23
得分:0 
这个问题已经在其他帖子中回答过了。

我的博客  http://wlb.wlb.blog.
2008-03-24 10:05
yms123
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:209
帖 子:12488
专家分:19042
注 册:2004-7-17
得分:0 
using System;
using System.Collections;
using
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using

namespace Application1
{
    /// <summary>
    /// 验证码生成页面
    /// </summary>
    public class VerifyCode : System.Web.UI.Page
    {
        private void Page_Load(object sender, System.EventArgs e)
        {
            // 在此处放置用户代码以初始化页面
            string vnum;
            vnum=GetByRndNum(6);
            Page.Session.Add("VerifyCode",vnum);
            Page.Session.Add("RegistCode",vnum);
            Response.ClearContent(); //需要输出图象信息 要修改HTTP头
            Response.ContentType="image/jpg";
            Response.BinaryWrite(GetByValidateCode(vnum).ToArray());
        }

        #region Web 窗体设计器生成的代码
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: 该调用是 Web 窗体设计器所必需的。
            //
            InitializeComponent();
            base.OnInit(e);
        }
        
        /// <summary>
        /// 设计器支持所需的方法 - 不要使用代码编辑器修改
        /// 此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {   
            this.Load += new System.EventHandler(this.Page_Load);

        }
        #endregion
        public MemoryStream GetByValidateCode(string VNum)//返回内存流
        {
            Bitmap Img = null;
            Graphics g = null ;
            MemoryStream ms = null ;
            Random random = new Random();
            int gheight = VNum.Length*12;
            Img = new Bitmap(gheight,24);
            g = Graphics.FromImage(Img);
            Font f = new Font("Arial", 12, FontStyle.Bold);
   
            g.Clear (GetByRandColor(204,255) );//设定背景色
            Pen blackPen = new Pen(Color.Ivory ,3);
            for (int i=0;i<155;i++)// 随机产生干扰线,使图象中的认证码不易被其它程序探测到
            {
                int x = random.Next (gheight);
                int y = random.Next (20);
                int xl = random.Next (6);
                int yl = random.Next (2);
                g.DrawLine (blackPen,x,y,x+xl,y+yl);
            }
            SolidBrush s = new  SolidBrush(Color.SandyBrown);
            g.DrawString(VNum,f,s,3,3);
            ms=new MemoryStream();
            Img.Save(ms,System.Drawing.Imaging.ImageFormat.Png);
            g.Dispose();
            Img.Dispose();
            return ms;
        }
        Color GetByRandColor(int fc,int bc)
        {
            Random random = new Random();
   
            if(fc>255) fc=255;
            if(bc>255) bc=255;
            //if(ac>255) ac=255;
            int r=fc+random.Next (bc-fc);
            int g=fc+random.Next (bc-fc);
            int b=fc+random.Next (bc-bc);
            Color rs = Color.FromArgb (r,g,b);
            return rs;
        }
        private string GetByRndNum(int VcodeNum)
        {
            string Vchar = "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,w,x,y,z" ;
            string[] VcArray = Vchar.Split(',') ;
            string  VNum = "" ;
            int temp = -1 ;//记录上次随机数值,尽量避免生产几个一样的随机数
            //采用一个简单的算法以保证生成随机数的不同
            Random rand =new Random();
            for ( int i = 1 ; i < VcodeNum+1 ; i++ )
            {   
                if ( temp != -1)
                {
                    rand =new Random(i*temp*unchecked((int)DateTime.Now.Ticks));
                }   
                int t =  rand.Next(35) ;
                if (temp != -1 && temp == t)
                {
                    return GetByRndNum( VcodeNum );
                }
                temp = t  ;
                VNum += VcArray[t];
            }
            return VNum ;
        }
    }

}
提供一个我现在用的随机验证码的代码
2008-03-24 15:13
Saber0227
Rank: 1
等 级:新手上路
帖 子:15
专家分:0
注 册:2008-3-19
得分:0 
Response.BinaryWrite(GetByValidateCode(vnum).ToArray());
LS的朋友。
这句代码貌似有点问题,能帮忙解决一下吗?
2008-03-24 16:40
寂寞的刺猬
Rank: 1
等 级:新手上路
帖 子:111
专家分:0
注 册:2008-3-23
得分:0 
重复发帖,郁闷中。。。

我的博客  http://wlb.wlb.blog.
2008-03-24 22:24
hnjie0000
Rank: 1
来 自:河南
等 级:新手上路
帖 子:10
专家分:0
注 册:2010-12-4
得分:0 
给你个样例,我从网上弄的,太有用了啊
um7cylyP.rar (5.37 KB)
2010-12-04 16:22
hnjie0000
Rank: 1
来 自:河南
等 级:新手上路
帖 子:10
专家分:0
注 册:2010-12-4
得分:0 
还有源代码
程序代码:
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;



public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        CreateCheckCode(GenerateCheckCode());
     
    }
private string GenerateCheckCode()
    {
        string checkCode;
        Random rd = new Random(DateTime.Now.Millisecond);//使用当前时间的毫秒为随机种子可避免生成相同的随机数
        checkCode = rd.Next(100000, 999999).ToString();
        Session["checkCode"] = checkCode;
        Response.Cookies.Add(new HttpCookie("checkCode", checkCode));
        return checkCode;
    }

    private void CreateCheckCode(string checkCode)
    {
        if (checkCode == null || checkCode.Trim() == String.Empty)
        {
            return;
        }

  System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 22);
  Graphics g = Graphics.FromImage(image);

        try
        {
            //生成随机生成器
            Random random = new Random();

            //清空图片背景色
            g.Clear(Color.White);

            //画图片的背景噪音线
            //for (int i = 0; i < 2; i++)
            //{
            //    int x1 = random.Next(image.Width);
            //    int x2 = random.Next(image.Width);
            //    int y1 = random.Next(image.Height);
            //    int y2 = random.Next(image.Height);

            //    g.DrawLine(new Pen(Color.Black), x1, y1, x2, y2);
            //}

            Font font = new System.Drawing.Font("Arial", 12, (System.Drawing.FontStyle.Bold));
            System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true);
            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.FromArgb(random.Next()));
            //}

            //画图片的边框线
            g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);

             ms = new ();
            image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
            Response.ClearContent();
            Response.ContentType = "image/Gif";
            Response.BinaryWrite(ms.ToArray());
        }
        finally
        {
            g.Dispose();
            image.Dispose();
        }
    }



2010-12-04 16:25



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-204951-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.050757 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved