Ejemplo n.º 1
0
        private static IdentityCard Parse2(String card)
        {
            var area = card.Substring(0, 6);

            var 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 XException("验证码校验失败!");
            }

            return idc;
        }
Ejemplo n.º 2
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);
                }

                var idc = Parse2(card);
                cache.Add(card, idc);
                return(idc);
            }
        }
Ejemplo n.º 3
0
        private static IdentityCard Parse2(String card)
        {
            var area = card.Substring(0, 6);

            var idc = new IdentityCard
            {
                AreaNum = ParseArea(area)
            };

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

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

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

            return(idc);
        }