예제 #1
0
        /// <summary>
        /// 多个登录设置缓存
        /// </summary>
        /// <param name="token"></param>
        /// <param name="userid"></param>
        public static void SetUserToken(string token, int userid)
        {
            Dictionary <int, List <string> > dicusers = CacheUtil.Get <Dictionary <int, List <string> > >("UserToken");

            if (dicusers == null)
            {
                dicusers = new Dictionary <int, List <string> >();
            }
            List <string> listtokens = new List <string>();

            if (dicusers.ContainsKey(userid))
            {
                listtokens = dicusers[userid];
                if (listtokens.Count <= 0)
                {
                    listtokens.Add(token);
                }
                else
                {
                    if (!AprilConfig.AllowMuiltiLogin)
                    {
                        foreach (var item in listtokens)
                        {
                            RemoveToken(item);
                        }
                        listtokens.Add(token);
                    }
                    else
                    {
                        bool isAdd = true;
                        foreach (var item in listtokens)
                        {
                            if (item == token)
                            {
                                isAdd = false;
                            }
                        }
                        if (isAdd)
                        {
                            listtokens.Add(token);
                        }
                    }
                }
            }
            else
            {
                listtokens.Add(token);
                dicusers.Add(userid, listtokens);
            }
            CacheUtil.Add("UserToken", dicusers, new TimeSpan(6, 0, 0), true);
        }
예제 #2
0
        private static void Add(string token, AdminEntity admin)
        {
            switch (AprilConfig.TokenCacheType)
            {
            //不推荐Cookie
            case AprilEnums.TokenCacheType.Cookie:
                CookieUtil.Add(token, admin);
                break;

            case AprilEnums.TokenCacheType.Cache:
                CacheUtil.Add(token, admin, new TimeSpan(0, 30, 0));
                break;

            case AprilEnums.TokenCacheType.Session:
                SessionUtil.Add(token, admin);
                break;

            case AprilEnums.TokenCacheType.Redis:
                RedisUtil.Add(token, admin);
                break;
            }
        }