现在的位置: 首页 > 综合 > 正文

验证码制作之三: 通过ASCEE码随机生成4位字符与数字验证码

2012年03月30日 ⁄ 综合 ⁄ 共 738字 ⁄ 字号 评论关闭
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;

/// <summary>
/// ValidateCode 的摘要说明
/// </summary>
public class ValidateCode
{
 public ValidateCode()
 {
 }

    //通过ASCEE码随机生成4位字符串组成验证码
    public string validateCode()
    {
        //
        byte[] bytes = new byte[100];
        Random randObj = new Random();
        int code;
        for (int i = 0; i < 4; i++)
        {
            code = randObj.Next(44, 122);
            bytes[i] = Convert.ToByte(code);
        }
        ASCIIEncoding ascii = new ASCIIEncoding();
        string validateCode = ascii.GetString(bytes,0,4);
        return validateCode.ToString();
    }
}

抱歉!评论已关闭.