[Ignore]    // TODO: The test is currently failing.
        public async Task LoggerTest()
        {
            for (int i = 0; i < 2; i++)
            {
                AdalTrace.Level = AdalTraceLevel.Informational;
                string guidValue = Guid.NewGuid().ToString();
                Logger.Information(null, "{0}", guidValue);
                StorageFolder sf = ApplicationData.Current.LocalFolder;
                AdalTrace.Level = AdalTraceLevel.None;
                StorageFile file = await sf.GetFileAsync("AdalTraces.log");

                try
                {
                    string content = await FileIO.ReadTextAsync(file);

                    Log.Comment(content);
                    Verify.IsTrue(content.Contains(guidValue));
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        }
Example #2
0
        internal AuthenticationResult LoadFromCache(string authority, string resource, string clientId, TokenSubjectType subjectType, string uniqueId, string displayableId, CallState callState)
        {
            lock (cacheLock)
            {
                Logger.Verbose(callState, "Looking up cache for a token...");

                AuthenticationResult result = null;

                KeyValuePair <TokenCacheKey, AuthenticationResult>?kvp = this.LoadSingleItemFromCache(authority,
                                                                                                      resource, clientId, subjectType, uniqueId, displayableId, callState);

                if (kvp.HasValue)
                {
                    TokenCacheKey cacheKey = kvp.Value.Key;
                    result = kvp.Value.Value;
                    bool tokenNearExpiry = (result.ExpiresOn <=
                                            DateTime.UtcNow + TimeSpan.FromMinutes(ExpirationMarginInMinutes));

                    if (tokenNearExpiry)
                    {
                        result.AccessToken = null;
                        Logger.Verbose(callState, "An expired or near expiry token was found in the cache");
                    }
                    else if (!cacheKey.ResourceEquals(resource))
                    {
                        Logger.Verbose(callState,
                                       string.Format(
                                           "Multi resource refresh token for resource '{0}' will be used to acquire token for '{1}'",
                                           cacheKey.Resource, resource));
                        var newResult = new AuthenticationResult(null, null, result.RefreshToken,
                                                                 DateTimeOffset.MinValue);
                        newResult.UpdateTenantAndUserInfo(result.TenantId, result.IdToken, result.UserInfo);
                        result = newResult;
                    }
                    else
                    {
                        Logger.Verbose(callState,
                                       string.Format("{0} minutes left until token in cache expires",
                                                     (result.ExpiresOn - DateTime.UtcNow).TotalMinutes));
                    }

                    if (result.AccessToken == null && result.RefreshToken == null)
                    {
                        this.tokenCacheDictionary.Remove(cacheKey);
                        Logger.Information(callState, "An old item was removed from the cache");
                        this.HasStateChanged = true;
                        result = null;
                    }

                    if (result != null)
                    {
                        Logger.Information(callState,
                                           "A matching item (access token or refresh token or both) was found in the cache");
                    }
                }
                else
                {
                    Logger.Information(callState, "No matching token was found in the cache");
                }

                return(result);
            }
        }
 static AuthenticationContext()
 {
     Logger.Information(null, string.Format("ADAL {0} with assembly version '{1}', file version '{2}' and informational version '{3}' is running...",
                                            PlatformSpecificHelper.GetProductName(), AdalIdHelper.GetAdalVersion(), AdalIdHelper.GetAssemblyFileVersion(), AdalIdHelper.GetAssemblyInformationalVersion()));
 }