https://www.cnblogs.com/vchenpeng/archive/2013/05/12/3074887.html
/// <summary>
/// 获取时间戳
/// </summary>
/// <returns></returns>
public static string GetTimeStamp()
{
TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
return Convert.ToInt64(ts.TotalSeconds).ToString();
}
/// <summary>
/// 获取验证码
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static string getCode(this string str)
{
int number;
char code;
string checkCode = String.Empty;
Random random = new Random();
foreach (char c in str)
{
number = random.Next();
if (c % 2 == 0)
{
code = (char)('0' + (char)(number % 10));
if (System.Text.RegularExpressions.Regex.IsMatch(code.ToString(), "^[0-9]+$"))
{
checkCode += code;
}
}
else
{
code = (char)('C' + (char)(number % 26));
if (System.Text.RegularExpressions.Regex.IsMatch(code.ToString(), "^[A-Z]+$"))
{
checkCode += code;
}
}
}
return checkCode;
}
/// <summary>
2 /// 动态生成指定数目的随机数或字母
3 /// </summary>
4 /// <param name="num">整数</param>
5 /// <returns>返回验证码字符串</returns>
6 private string GenerateCheckCode(int num)
7 {
8 int number; //定义变量
9 char code;
10 string checkCode = String.Empty; //空字符串,只读
11 Random random = new Random(); //定义随机变量实例
12 for (int i=0; i < num;i++ )
13 {
14 //利用for循环生成指定数目的随机数或字母
15 number = random.Next(); //返回一个小于指定的最大值的非负的随机数 next有三个构造函数
16 if (number % 2 == 0)
17 {
18 //产生一个一位数
19 code = (char)('0' + (char)(number % 10));
20 }
21 else
22 {
23 //产生一个字母
24 code = (char)('C'+(char)(number % 26));
25 }
26 checkCode += code.ToString();
27 }
28 return checkCode;
29 }