Exemple #1
0
        /// <summary>使用身份证号码初始化</summary>
        /// <param name="card"></param>
        /// <returns></returns>
        public static IdentityCard Parse(String card)
        {
            if (String.IsNullOrEmpty(card))
            {
                return(null);
            }
            if (card.Length != 15 && card.Length != 18)
            {
                return(null);
            }

            //转为小写,18位身份证后面有个字母x
            card = card.ToLower();

            IdentityCard ic = null;

            if (cache.TryGetValue(card, out ic))
            {
                return(ic);
            }
            lock (cache)
            {
                if (cache.TryGetValue(card, out ic))
                {
                    return(ic);
                }

                IdentityCard idc = Parse2(card);
                cache.Add(card, idc);
                return(idc);
            }
        }
Exemple #2
0
        private static IdentityCard Parse2(String card)
        {
            String area = card.Substring(0, 6);

            IdentityCard idc = new IdentityCard();

            idc.AreaNum = ParseArea(area);

            if (card.Length == 15)
            {
                idc.ParseBirthdayAndSex15(card);
            }
            else if (card.Length == 18)
            {
                idc.ParseBirthdayAndSex18(card);

                //校验码验证  GB11643-1999标准
                String[] arrVarifyCode = ("1,0,x,9,8,7,6,5,4,3,2").Split(',');
                Int32[]  Wi            = new Int32[] { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };
                char[]   Ai            = card.Remove(17).ToCharArray();
                int      sum           = 0;
                for (int i = 0; i < 17; i++)
                {
                    sum += Wi[i] * (Ai[i] - '0');
                }

                int y = -1;
                Math.DivRem(sum, 11, out y);
                if (arrVarifyCode[y] != card.Substring(17, 1).ToLower())
                {
                    throw new Exception("验证码校验失败!");
                }
            }

            return(idc);
        }