Example #1
0
        /// <summary>
        /// 获取AccessToken
        /// </summary>
        /// <returns></returns>
        public static string GetAccessToken()
        {
            QyAccessToken accessToken = null;
            bool          isGet       = false;

            if (CacheUtil.Exists("QyAccessToken"))
            {
                accessToken = CacheUtil.Get <QyAccessToken>("QyAccessToken");
                if (accessToken.Expire_Time >= DateTime.Now.AddMinutes(1))
                {
                    isGet = true;
                }
            }
            if (!isGet)
            {
                string url = $"https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={CorpID}&corpsecret={Secret}";
                //请求获取
                string res = RequestUtil.HttpGet(url);
                accessToken = JsonConvert.DeserializeObject <QyAccessToken>(res);
                if (accessToken != null && accessToken.ErrCode == 0)
                {
                    accessToken.Expire_Time = DateTime.Now.AddSeconds(accessToken.Expires_In);
                    CacheUtil.Set("QyAccessToken", accessToken, new TimeSpan(2, 0, 0));
                }
                else
                {
                    LogUtil.Error($"获取accesstoken失败——{accessToken.ErrCode},{accessToken.ErrMsg}");
                }
            }

            return(accessToken.Access_Token);
        }
Example #2
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);
        }
Example #3
0
        /// <summary>
        /// 多个登录获取
        /// </summary>
        /// <param name="userid"></param>
        /// <returns></returns>
        public static List <string> GetUserToken(int userid)
        {
            Dictionary <int, List <string> > dicusers = CacheUtil.Get <Dictionary <int, List <string> > >("UserToken");
            List <string> lists = new List <string>();

            if (dicusers != null && dicusers.Count > 0)
            {
                foreach (var item in dicusers)
                {
                    if (item.Key == userid)
                    {
                        lists = dicusers[userid];
                        break;
                    }
                }
            }
            return(lists);
        }
Example #4
0
        /// <summary>
        /// 多个登录删除缓存
        /// </summary>
        /// <param name="userid"></param>
        public static void RemoveUserToken(int userid)
        {
            Dictionary <int, List <string> > dicusers = CacheUtil.Get <Dictionary <int, List <string> > >("UserToken");

            if (dicusers != null && dicusers.Count > 0)
            {
                if (dicusers.ContainsKey(userid))
                {
                    //删除所有token
                    var listtokens = dicusers[userid];
                    foreach (var token in listtokens)
                    {
                        RemoveToken(token);
                    }
                    dicusers.Remove(userid);
                }
            }
        }
Example #5
0
        private static void Remove(string token)
        {
            switch (AprilConfig.TokenCacheType)
            {
            case AprilEnums.TokenCacheType.Cookie:
                CookieUtil.Remove(token);
                break;

            case AprilEnums.TokenCacheType.Cache:
                CacheUtil.Remove(token);
                break;

            case AprilEnums.TokenCacheType.Session:
                SessionUtil.Remove(token);
                break;

            case AprilEnums.TokenCacheType.Redis:
                RedisUtil.Remove(token);
                break;
            }
        }
Example #6
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;
            }
        }
Example #7
0
        private static AdminEntity Get(string token)
        {
            AdminEntity admin = null;

            switch (AprilConfig.TokenCacheType)
            {
            case AprilEnums.TokenCacheType.Cookie:
                admin = CookieUtil.Get <AdminEntity>(token);
                break;

            case AprilEnums.TokenCacheType.Cache:
                admin = CacheUtil.Get <AdminEntity>(token);
                break;

            case AprilEnums.TokenCacheType.Session:
                admin = SessionUtil.Get <AdminEntity>(token);
                break;

            case AprilEnums.TokenCacheType.Redis:
                admin = RedisUtil.Get <AdminEntity>(token);
                break;
            }
            return(admin);
        }