private async Task <TokenInfo> AuthenticationResultAsync(IKeyValueSettings settings, IPlatformParameters platformParameters) { var authContext = GetContext(settings, out string serviceId, out string clientId, out string authority); var redirectUri = new Uri(settings.Values[TokenKeys.RedirectUrl]); Logger.Technical().From <AdalTokenProvider>().System($"{TokenKeys.RedirectUrl} = {redirectUri}.").Log(); Logger.Technical().From <AdalTokenProvider>().System("Acquire a token.").Log(); // Start Vpn if needed. Network.Handler.OnCalling?.Invoke(new Uri(authority)); AuthenticationResult result = null; // Check if we have an AuthenticationResult cached and still valid. if (_resultCache.ContainsKey(clientId)) { result = _resultCache[clientId]; // Is valid with a security margin of 1 minute. if (null == result || result.ExpiresOn.LocalDateTime.AddMinutes(-1) < DateTime.Now) { Logger.Technical().From <AdalTokenProvider>().System($"Token cached for clientId = {clientId} is expired. Is removed from the cache.").Log(); _resultCache.Remove(clientId); result = null; } } if (null == result) { result = await authContext.AcquireTokenAsync(serviceId, clientId, redirectUri, platformParameters); _resultCache.Add(clientId, result); Logger.Technical().From <AdalTokenProvider>().System($"Add the token in the cache for clientId = {clientId}.").Log(); } if (null != result) { // Dump no sensitive information. Logger.Technical().From <AdalTokenProvider>().System($"Token information for user {result.UserInfo.DisplayableId}.").Log(); Logger.Technical().From <AdalTokenProvider>().System($"Token expiration = {result.ExpiresOn.ToString("dd-MM-yyyy HH:mm:ss")}.").Log(); return(result.ToTokenInfo()); } return(null); }