// 验证码类型(0-字母数字混合,1-数字,2-字母)
private string validateCodeType = "0";
// 验证码字符个数
private int validateCodeCount = 4;
// 验证码的字符集,去掉了一些容易混淆的字符
char[] character = { \'2\', \'3\', \'4\', \'5\', \'6\', \'8\', \'9\', \'A\', \'B\', \'C\', \'D\', \'E\', \'F\', \'G\', \'H\', \'J\', \'K\', \'L\', \'M\', \'N\', \'P\', \'R\', \'S\', \'T\', \'W\', \'X\', \'Y\' };
private string GenerateCheckCode()
{
char code ;
string checkCode = String.Empty;
System.Random random = new Random();
for (int i = 0; i < validateCodeCount; i++)
{
code = character[random.Next(character.Length)];
// 要求全为数字或字母
if (validateCodeType == "1")
{
if ((int)code < 48 || (int)code > 57)
{
i--;
continue;
}
}
else if (validateCodeType == "2")
{
if ((int)code < 65 || (int)code > 90)
{
i--;
continue;
}
}
checkCode += code;
}
Response.Cookies.Add(new System.Web.HttpCookie("CheckCode", checkCode));
this.Session["CheckCode"] = checkCode;
return checkCode;
}