Exemplo n.º 1
0
        /// <summary>
        /// Get assicate data by thread safe way
        /// </summary>
        /// <param name="token">token</param>
        /// <returns>The associate object</returns>
        private MemoryTokenData GetAssociateData(string token)
        {
            MemoryTokenData tokenData = null;

            lock (this.tokens)
            {
                if (this.tokens.ContainsKey(token))
                {
                    tokenData = this.tokens[token];
                }
            }

            return(tokenData);
        }
Exemplo n.º 2
0
        /// <summary>
        /// The method is genarate token which associate with a object
        /// </summary>
        /// <param name="obj">the object which associate with the token</param>
        /// <returns>return a token</returns>
        public virtual string GeneToken(object obj)
        {
            string token = Guid.NewGuid().ToString("N");

            var tokenData = new MemoryTokenData();

            tokenData.LastAccessTime = DateTime.Now;
            tokenData.AssociateData  = obj;
            // because the Dictionary type is not thread safe object, so use lock to ensure thread safe
            lock (this.tokens)
            {
                this.tokens.Add(token, tokenData);
            }

            return(token);
        }
Exemplo n.º 3
0
        /// <summary>
        /// update token expired time
        /// basically, it will be invoke in Authorization Filter
        /// </summary>
        /// <param name="token">token</param>
        public virtual void RefreshToken(string token)
        {
            MemoryTokenData tokenData = this.GetAssociateData(token);

            if (tokenData != null)
            {
                // If the token didn't not expired. refresh the last access time
                // else remove the token from tokens
                if (tokenData.LastAccessTime.AddSeconds(this.timeout) >= DateTime.Now)
                {
                    tokenData.LastAccessTime = DateTime.Now;
                }
                else
                {
                    this.RemoveToken(token);
                }
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Get the associate object by token
        /// </summary>
        /// <typeparam name="T">The object type</typeparam>
        /// <param name="token">token</param>
        /// <returns>the assicate object will return if the token exist, the null will be return if the token didn't exist</returns>
        public virtual T GetAssociateObjectByToken <T>(string token)
        {
            T result = default(T);
            MemoryTokenData tokenData = this.GetAssociateData(token);

            if (tokenData != null)
            {
                // If the token didn't not expired. refresh the last access time and associate data
                // else remove the token from tokens
                if (tokenData.LastAccessTime.AddSeconds(this.timeout) >= DateTime.Now)
                {
                    if (tokenData.AssociateData != null)
                    {
                        result = (T)tokenData.AssociateData;
                    }
                }
                else
                {
                    this.RemoveToken(token);
                }
            }

            return(result);
        }