Exemplo n.º 1
0
 /// <summary>
 /// 检测token是否有效
 /// </summary>
 /// <param name="sid"></param>
 /// <returns></returns>
 public static Token TokenFromSid(long sid)
 {
     lock (lockObject)
     {
         //Logger.DebugFormat("dictToken:{0}", JsonConvert.SerializeObject(dictToken));
         //Logger.DebugFormat("dictIdSecretWithSid:{0}", JsonConvert.SerializeObject(dictIdSecretWithSid));
         if (dictToken.ContainsKey(sid))
         {
             Token token = dictToken[sid];
             if (token.ExpireTime > DateTime.Now)
             {
                 return(token);
             }
             else
             {
                 dictToken.Remove(sid);
                 return(null);
             }
         }
         else
         {
             return(AppSessionBLL.LoadToken(sid));
         }
     }
 }
Exemplo n.º 2
0
        public static string GetToken(string userId)
        {
            cleanExpired();

            string key = userId;

            lock (lockObject)
            {
                if (dictIdSecretWithSid.ContainsKey(key))
                {
                    return(dictIdSecretWithSid[key].ToString());
                }
            }
            long  sid   = LeafSnowflake.getID();
            Token token = new Token();

            token.UserID     = userId;
            token.ExpireTime = DateTime.Now.AddHours(2);

            AppSessionBLL.SaveToken(sid, token);

            lock (lockObject)
            {
                dictToken.Add(sid, token);
                dictIdSecretWithSid.Add(key, sid);
            }
            return(sid.ToString());
        }
Exemplo n.º 3
0
        public static string GetToken(long appId, string appSecret)
        {
            cleanExpired();

            string key = appId + "_" + appSecret;

            lock (lockObject)
            {
                if (dictIdSecretWithSid.ContainsKey(key))
                {
                    return(dictIdSecretWithSid[key].ToString());
                }
            }

            if (AppInfoBLL.Valid(appId, appSecret))
            {
                long sid = LeafSnowflake.getID();

                Token token = new Token();
                token.UserID     = appId.ToString();
                token.ExpireTime = DateTime.Now.AddHours(2);

                AppSessionBLL.SaveToken(sid, token);

                lock (lockObject)
                {
                    dictToken.Add(sid, token);
                    dictIdSecretWithSid.Add(key, sid);
                }

                return(sid.ToString());
            }
            else
            {
                return(null);
            }
        }