• C#后台生成验证码


    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         }
  • 相关阅读:
    (转) Hibernate检索方式概述
    (转) Hibernate注解开发
    (转)Hibernate关联映射——一对多(多对一)
    (转)学习淘淘商城第二十二课(KindEditor富文本编辑器的使用)
    (转)Hibernate关联映射——对象的三种关系
    (转)Hibernate的一级缓存
    (转) Hibernate持久化类与主键生成策略
    ( 转)Hibernate常用API
    (转)Hibernate的配置详解
    范仁义css3课程---22、float导致的父容器高度坍塌及解决
  • 原文地址:https://www.cnblogs.com/LuoEast/p/7886290.html
Copyright © 2020-2023  润新知