public async Task <string> GetAccessTokenAsync(CancellationToken cancellationToken = default) { return(await _cache.GetOrCreateAsync(CacheKey, async cacheEntry => { _logger.LogInformation("Cached AccessToken Expired"); var token = await _inner.GetAccessTokenAsync(cancellationToken); cacheEntry.SetAbsoluteExpiration(DateTimeOffset.UtcNow.AddMinutes(50)); _logger.LogInformation("Caching SQL Access Token for an hour"); return token; })); }
public override async Task <InterceptionResult> ConnectionOpeningAsync( DbConnection connection, ConnectionEventData eventData, InterceptionResult result, CancellationToken cancellationToken = default) { var sqlConnection = (SqlConnection)connection; if (_connectionNeedsAccessToken) { _logger.LogInformation("Getting AccessToken Async"); var token = await _tokenProvider.GetAccessTokenAsync(cancellationToken); sqlConnection.AccessToken = token; } else { _logger.LogWarning($"Skipping GetAccessToken Async because ConnectionNeedsAccessToken is {_connectionNeedsAccessToken}"); } return(await base.ConnectionOpeningAsync(connection, eventData, result, cancellationToken)); }