Beispiel #1
0
        public static LoginToken GetToken(String emailAddress)
        {
            if (!haveReadCacheFile)
            {
                ReadCacheFile();
                haveReadCacheFile = true;
            }

            LoginToken token;

            if (!Tokens.TryGetValue(emailAddress, out token))
            {
                return(null);
            }

            // Ensure the LoginToken is still valid.
            DateTime expirationTime = token.CreatedAt.ToLocalTime() + UnixTimeConverter.FromUnixTimeSpan(token.ExpiresIn);

            if (DateTime.Now + ExpirationTimeWindow >= expirationTime)
            {
                Tokens.Remove(emailAddress);
                WriteCacheFile();
                token = null;
            }
            return(token);
        }
        public Task <LoginToken> GetTokenAsync(string emailAddress)
        {
            LoginToken token = null;

            lock (CacheLock)
            {
                if (!_haveReadCacheFile && OSSupportsTokenCache)
                {
                    ReadCacheFile();
                    _haveReadCacheFile = true;
                }

                if (!Tokens.TryGetValue(emailAddress, out token))
                {
                    // Return a task with null
                    return(Task.FromResult <LoginToken>(null));
                }

                // Ensure the LoginToken is still valid.
                var expirationTime = token.CreatedUtc.ToLocalTime() + UnixTimeConverter.FromUnixTimeSpan(token.ExpiresIn);

                if (DateTime.Now + ExpirationTimeWindow >= expirationTime)
                {
                    Tokens.Remove(emailAddress);
                    if (OSSupportsTokenCache)
                    {
                        WriteCacheFile();
                    }
                    token = null;
                }
            }
            return(Task.FromResult(token));
        }