验证方法

1.先写一个验证方法
 public string CreateValidate(int count)
    {
        string allchar = "1,2,3,4,5,6,7,8,9,0,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,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";//写一个包含数字和字母的字符串
        string[] allchararray = allchar.Split(',');//字符串除去逗号用数组接收
        string randomcode = "";
        int temp = -1;
        Random rd = new Random();//创建随机数实例
        for (int i = 0; i < count; i++)
        {
            if (temp != -1)
            {
                rd = new Random(i * temp * ((int)DateTime.Now.Ticks));
            }
            int t = rd.Next(62);
            if (temp == t)
            {
                return CreateValidate(count);
            }
            temp = t;
            randomcode += allchararray[t];
        }
        Session["code"] = randomcode;//用Session保存随机数
        return randomcode;
    }
2.画一个图片
Bitmap bm = new Bitmap(80, 20);
        Graphics gp = Graphics.FromImage(bm);
        gp.Clear(Color.White);
        gp.DrawString(CreateValidate(4), new Font("宋体", 15), Brushes.Blue, new PointF(0, 0));
        gp.DrawLine(Pens.AliceBlue, new Point(0, 10), new Point(80, 10));
        gp.DrawLine(Pens.AliceBlue, new Point(0, 5), new Point(80, 10));
        bm.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg); //用图片对象,将对象本身保存为服务端向客户端浏览器的响应输出流,并指定文件类型。
        Response.Write(CreateValidate(4));
3.转换
Session["code"].ToString().ToLower() //字母转换成小写
 Session["code"].ToString().ToUpper()//字母转换成大写
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章