示例#1
0
        /// <summary>
        /// 生成令牌
        /// </summary>
        /// <param name="JobNo"></param>
        public static string CreateToKen(string JobNo)
        {
            string accessToken = SSOHelper.Encrypt(string.Format("{0}|{1}", JobNo, Guid.NewGuid().ToString()));

            lock (_lock)
            {
                ToKenHelper.ToKenList.Where(c => c.JobNo == JobNo).ToList().ForEach(x => { ToKenHelper.ToKenList.Remove(x); });
                AccessToKen model = new AccessToKen();
                model.ToKen     = accessToken;
                model.JobNo     = JobNo;
                model.BeginTime = DateTime.Now;
                model.EndTime   = DateTime.Now.AddHours(2);
                ToKenHelper.ToKenList.Add(model);
            }
            return(accessToken);
        }
示例#2
0
        /// <summary>
        /// 令牌验证
        /// </summary>
        /// <param name="accessToKen"></param>
        /// <returns></returns>
        public static bool CheckAccessToKen(string accessToKen)
        {
            bool flag = false;

            if (string.IsNullOrWhiteSpace(accessToKen))
            {
                return(flag);
            }
            string token = SSOHelper.Decrypt(accessToKen);

            if (!token.Contains("|"))
            {
                return(flag);
            }
            string[] result = token.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
            if (result.Length != 2)
            {
                return(flag);
            }

            lock (_lock)
            {
                AccessToKen model = ToKenHelper.ToKenList.Where(c => c.ToKen == accessToKen).SingleOrDefault();
                if (model != null)
                {
                    if (model.BeginTime <= DateTime.Now && DateTime.Now <= model.EndTime)
                    {
                        int index = ToKenHelper.ToKenList.IndexOf(model);
                        ToKenHelper.ToKenList[index].BeginTime = DateTime.Now;
                        ToKenHelper.ToKenList[index].EndTime   = DateTime.Now.AddHours(2);
                        flag = true;
                    }
                    else
                    {
                        ToKenHelper.ToKenList.Where(c => c.ToKen == accessToKen).ToList().ForEach(x => { ToKenHelper.ToKenList.Remove(x); });
                    }
                }
            }


            return(flag);
        }